@@ -25,41 +25,41 @@ |
||
25 | 25 | */ |
26 | 26 | class PlaceMapperService |
27 | 27 | { |
28 | - private ModuleService $module_service; |
|
28 | + private ModuleService $module_service; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor for PlaceMapperService |
|
32 | - * |
|
33 | - * @param ModuleService $module_service |
|
34 | - */ |
|
35 | - public function __construct(ModuleService $module_service) |
|
36 | - { |
|
37 | - $this->module_service = $module_service; |
|
38 | - } |
|
30 | + /** |
|
31 | + * Constructor for PlaceMapperService |
|
32 | + * |
|
33 | + * @param ModuleService $module_service |
|
34 | + */ |
|
35 | + public function __construct(ModuleService $module_service) |
|
36 | + { |
|
37 | + $this->module_service = $module_service; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get all place mappers available. |
|
42 | - * |
|
43 | - * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface} |
|
44 | - * |
|
45 | - * @param bool $include_disabled |
|
46 | - * @return Collection<PlaceMapperInterface> |
|
47 | - */ |
|
48 | - public function all(bool $include_disabled = false): Collection |
|
49 | - { |
|
50 | - /** @var Collection<PlaceMapperInterface> $place_mappers */ |
|
51 | - $place_mappers = $this->module_service |
|
52 | - ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled) |
|
53 | - ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers()) |
|
54 | - ->map(static function (string $mapper_class): ?PlaceMapperInterface { |
|
55 | - try { |
|
56 | - $mapper = app($mapper_class); |
|
57 | - return $mapper instanceof PlaceMapperInterface ? $mapper : null; |
|
58 | - } catch (BindingResolutionException $ex) { |
|
59 | - return null; |
|
60 | - } |
|
61 | - })->filter(); |
|
40 | + /** |
|
41 | + * Get all place mappers available. |
|
42 | + * |
|
43 | + * {@internal The list is generated based on the modules exposing ModulePlaceMapperProviderInterface} |
|
44 | + * |
|
45 | + * @param bool $include_disabled |
|
46 | + * @return Collection<PlaceMapperInterface> |
|
47 | + */ |
|
48 | + public function all(bool $include_disabled = false): Collection |
|
49 | + { |
|
50 | + /** @var Collection<PlaceMapperInterface> $place_mappers */ |
|
51 | + $place_mappers = $this->module_service |
|
52 | + ->findByInterface(ModulePlaceMapperProviderInterface::class, $include_disabled) |
|
53 | + ->flatMap(fn(ModulePlaceMapperProviderInterface $module) => $module->listPlaceMappers()) |
|
54 | + ->map(static function (string $mapper_class): ?PlaceMapperInterface { |
|
55 | + try { |
|
56 | + $mapper = app($mapper_class); |
|
57 | + return $mapper instanceof PlaceMapperInterface ? $mapper : null; |
|
58 | + } catch (BindingResolutionException $ex) { |
|
59 | + return null; |
|
60 | + } |
|
61 | + })->filter(); |
|
62 | 62 | |
63 | - return $place_mappers; |
|
64 | - } |
|
63 | + return $place_mappers; |
|
64 | + } |
|
65 | 65 | } |
@@ -26,64 +26,64 @@ |
||
26 | 26 | */ |
27 | 27 | class GeoAnalysisDataService |
28 | 28 | { |
29 | - /** |
|
30 | - * Yields indviduals and family records for a specified tree. |
|
31 | - * |
|
32 | - * @param Tree $tree |
|
33 | - * @return \Generator<\Fisharebest\Webtrees\GedcomRecord> |
|
34 | - */ |
|
35 | - public function individualsAndFamilies(Tree $tree): Generator |
|
36 | - { |
|
37 | - yield from DB::table('individuals') |
|
38 | - ->where('i_file', '=', $tree->id()) |
|
39 | - ->select(['individuals.*']) |
|
40 | - ->get() |
|
41 | - ->map(Registry::individualFactory()->mapper($tree)) |
|
42 | - ->filter(GedcomRecord::accessFilter()) |
|
43 | - ->all(); |
|
29 | + /** |
|
30 | + * Yields indviduals and family records for a specified tree. |
|
31 | + * |
|
32 | + * @param Tree $tree |
|
33 | + * @return \Generator<\Fisharebest\Webtrees\GedcomRecord> |
|
34 | + */ |
|
35 | + public function individualsAndFamilies(Tree $tree): Generator |
|
36 | + { |
|
37 | + yield from DB::table('individuals') |
|
38 | + ->where('i_file', '=', $tree->id()) |
|
39 | + ->select(['individuals.*']) |
|
40 | + ->get() |
|
41 | + ->map(Registry::individualFactory()->mapper($tree)) |
|
42 | + ->filter(GedcomRecord::accessFilter()) |
|
43 | + ->all(); |
|
44 | 44 | |
45 | - yield from DB::table('families') |
|
46 | - ->where('f_file', '=', $tree->id()) |
|
47 | - ->select(['families.*']) |
|
48 | - ->get() |
|
49 | - ->map(Registry::familyFactory()->mapper($tree)) |
|
50 | - ->filter(GedcomRecord::accessFilter()) |
|
51 | - ->all(); |
|
52 | - } |
|
45 | + yield from DB::table('families') |
|
46 | + ->where('f_file', '=', $tree->id()) |
|
47 | + ->select(['families.*']) |
|
48 | + ->get() |
|
49 | + ->map(Registry::familyFactory()->mapper($tree)) |
|
50 | + ->filter(GedcomRecord::accessFilter()) |
|
51 | + ->all(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest |
|
56 | - * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first). |
|
57 | - * |
|
58 | - * {@internal The places are taken only from the individuals and families records.} |
|
59 | - * |
|
60 | - * @param Tree $tree |
|
61 | - * @return array<int, string[]> |
|
62 | - */ |
|
63 | - public function placeHierarchyExample(Tree $tree): array |
|
64 | - { |
|
65 | - $query_individuals = DB::table('individuals') |
|
66 | - ->select(['i_gedcom AS g_gedcom']) |
|
67 | - ->where('i_file', '=', $tree->id()) |
|
68 | - ->where('i_gedcom', 'like', '%2 PLAC %'); |
|
54 | + /** |
|
55 | + * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest |
|
56 | + * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first). |
|
57 | + * |
|
58 | + * {@internal The places are taken only from the individuals and families records.} |
|
59 | + * |
|
60 | + * @param Tree $tree |
|
61 | + * @return array<int, string[]> |
|
62 | + */ |
|
63 | + public function placeHierarchyExample(Tree $tree): array |
|
64 | + { |
|
65 | + $query_individuals = DB::table('individuals') |
|
66 | + ->select(['i_gedcom AS g_gedcom']) |
|
67 | + ->where('i_file', '=', $tree->id()) |
|
68 | + ->where('i_gedcom', 'like', '%2 PLAC %'); |
|
69 | 69 | |
70 | - $query_families = DB::table('families') |
|
71 | - ->select(['f_gedcom AS g_gedcom']) |
|
72 | - ->where('f_file', '=', $tree->id()) |
|
73 | - ->where('f_gedcom', 'like', '%2 PLAC %'); |
|
70 | + $query_families = DB::table('families') |
|
71 | + ->select(['f_gedcom AS g_gedcom']) |
|
72 | + ->where('f_file', '=', $tree->id()) |
|
73 | + ->where('f_gedcom', 'like', '%2 PLAC %'); |
|
74 | 74 | |
75 | - return $query_individuals->unionAll($query_families) |
|
76 | - ->get()->pluck('g_gedcom') |
|
77 | - ->flatMap(static function (string $gedcom): array { |
|
78 | - preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches); |
|
79 | - return $matches[1] ?? []; |
|
80 | - }) |
|
81 | - ->sort(I18N::comparator())->reverse() |
|
82 | - ->mapWithKeys(static function (string $place): array { |
|
83 | - $place_array = array_reverse(array_filter(array_map('trim', explode(",", $place)))); |
|
84 | - return [ count($place_array) => $place_array ]; |
|
85 | - }) |
|
86 | - ->sortKeys() |
|
87 | - ->last(); |
|
88 | - } |
|
75 | + return $query_individuals->unionAll($query_families) |
|
76 | + ->get()->pluck('g_gedcom') |
|
77 | + ->flatMap(static function (string $gedcom): array { |
|
78 | + preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches); |
|
79 | + return $matches[1] ?? []; |
|
80 | + }) |
|
81 | + ->sort(I18N::comparator())->reverse() |
|
82 | + ->mapWithKeys(static function (string $place): array { |
|
83 | + $place_array = array_reverse(array_filter(array_map('trim', explode(",", $place)))); |
|
84 | + return [ count($place_array) => $place_array ]; |
|
85 | + }) |
|
86 | + ->sortKeys() |
|
87 | + ->last(); |
|
88 | + } |
|
89 | 89 | } |
@@ -24,67 +24,67 @@ |
||
24 | 24 | */ |
25 | 25 | trait PlaceMapperTrait |
26 | 26 | { |
27 | - private ?PlaceMapperConfigInterface $config = null; |
|
27 | + private ?PlaceMapperConfigInterface $config = null; |
|
28 | 28 | |
29 | - /** @var array<string, mixed> $data */ |
|
30 | - private array $data = []; |
|
29 | + /** @var array<string, mixed> $data */ |
|
30 | + private array $data = []; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Implementation of PlaceMapperInterface::boot |
|
34 | - * |
|
35 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot() |
|
36 | - */ |
|
37 | - public function boot(): void |
|
38 | - { |
|
39 | - } |
|
32 | + /** |
|
33 | + * Implementation of PlaceMapperInterface::boot |
|
34 | + * |
|
35 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::boot() |
|
36 | + */ |
|
37 | + public function boot(): void |
|
38 | + { |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Implementation of PlaceMapperInterface::config |
|
43 | - * |
|
44 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config() |
|
45 | - * |
|
46 | - * @return PlaceMapperConfigInterface |
|
47 | - */ |
|
48 | - public function config(): PlaceMapperConfigInterface |
|
49 | - { |
|
50 | - return $this->config ?? new NullPlaceMapperConfig(); |
|
51 | - } |
|
41 | + /** |
|
42 | + * Implementation of PlaceMapperInterface::config |
|
43 | + * |
|
44 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::config() |
|
45 | + * |
|
46 | + * @return PlaceMapperConfigInterface |
|
47 | + */ |
|
48 | + public function config(): PlaceMapperConfigInterface |
|
49 | + { |
|
50 | + return $this->config ?? new NullPlaceMapperConfig(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Implementation of PlaceMapperInterface::setConfig |
|
55 | - * |
|
56 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig() |
|
57 | - * |
|
58 | - * @param PlaceMapperConfigInterface $config |
|
59 | - */ |
|
60 | - public function setConfig(PlaceMapperConfigInterface $config): void |
|
61 | - { |
|
62 | - $this->config = $config; |
|
63 | - } |
|
53 | + /** |
|
54 | + * Implementation of PlaceMapperInterface::setConfig |
|
55 | + * |
|
56 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setConfig() |
|
57 | + * |
|
58 | + * @param PlaceMapperConfigInterface $config |
|
59 | + */ |
|
60 | + public function setConfig(PlaceMapperConfigInterface $config): void |
|
61 | + { |
|
62 | + $this->config = $config; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Implementation of PlaceMapperInterface::data |
|
67 | - * |
|
68 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data() |
|
69 | - * |
|
70 | - * @param string $key |
|
71 | - * @return NULL|mixed |
|
72 | - */ |
|
73 | - public function data(string $key) |
|
74 | - { |
|
75 | - return $this->data[$key] ?? null; |
|
76 | - } |
|
65 | + /** |
|
66 | + * Implementation of PlaceMapperInterface::data |
|
67 | + * |
|
68 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::data() |
|
69 | + * |
|
70 | + * @param string $key |
|
71 | + * @return NULL|mixed |
|
72 | + */ |
|
73 | + public function data(string $key) |
|
74 | + { |
|
75 | + return $this->data[$key] ?? null; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Implementation of PlaceMapperInterface::setData |
|
80 | - * |
|
81 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData() |
|
82 | - * |
|
83 | - * @param string $key |
|
84 | - * @param mixed|null $data |
|
85 | - */ |
|
86 | - public function setData(string $key, $data): void |
|
87 | - { |
|
88 | - $this->data[$key] = $data; |
|
89 | - } |
|
78 | + /** |
|
79 | + * Implementation of PlaceMapperInterface::setData |
|
80 | + * |
|
81 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::setData() |
|
82 | + * |
|
83 | + * @param string $key |
|
84 | + * @param mixed|null $data |
|
85 | + */ |
|
86 | + public function setData(string $key, $data): void |
|
87 | + { |
|
88 | + $this->data[$key] = $data; |
|
89 | + } |
|
90 | 90 | } |
@@ -36,184 +36,184 @@ |
||
36 | 36 | */ |
37 | 37 | class CoordinatesPlaceMapper implements PlaceMapperInterface |
38 | 38 | { |
39 | - use PlaceMapperTrait; |
|
40 | - |
|
41 | - private ?string $cache_key = null; |
|
42 | - |
|
43 | - /** |
|
44 | - * {@inheritDoc} |
|
45 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::title() |
|
46 | - */ |
|
47 | - public function title(): string |
|
48 | - { |
|
49 | - return I18N::translate('Mapping on place coordinates'); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * {@inheritDoc} |
|
54 | - * |
|
55 | - * {@internal The Place is associated to a Point only. |
|
56 | - * PlaceLocation can calculate a BoundingBox. |
|
57 | - * Using a BoundingBox could make the mapping more complex and potentially arbitary. |
|
58 | - * Furthermore, when no coordinate is found for the place or its children, then it bubbles up to the parents. |
|
59 | - * This could create the unwanted side effect of a very large area to consider} |
|
60 | - * |
|
61 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::map() |
|
62 | - */ |
|
63 | - public function map(Place $place, string $feature_property): ?string |
|
64 | - { |
|
65 | - $location = new PlaceLocation($place->gedcomName()); |
|
66 | - $longitude = $location->longitude(); |
|
67 | - $latitude = $location->latitude(); |
|
68 | - if ($longitude === null || $latitude === null) { |
|
69 | - return null; |
|
70 | - } |
|
71 | - |
|
72 | - $features_index = $this->featuresIndex(); |
|
73 | - if ($features_index === null) { |
|
74 | - return null; |
|
75 | - } |
|
76 | - |
|
77 | - $place_point = Point::xy($longitude, $latitude, $features_index['SRID']); |
|
78 | - $grid_box = $this->getGridCell( |
|
79 | - $place_point, |
|
80 | - $features_index['map_NE'], |
|
81 | - $features_index['map_SW'], |
|
82 | - $features_index['nb_columns'] |
|
83 | - ); |
|
84 | - if ($grid_box === null || !$this->setGeometryEngine()) { |
|
85 | - return null; |
|
86 | - } |
|
87 | - $features = $features_index['grid'][$grid_box[0]][$grid_box[1]]; |
|
88 | - foreach ($features as $feature) { |
|
89 | - $geometry = $feature->getGeometry(); |
|
90 | - if ($geometry !== null && $place_point->SRID() === $geometry->SRID() && $geometry->contains($place_point)) { |
|
91 | - return $feature->getProperty($feature_property); |
|
92 | - } |
|
93 | - } |
|
94 | - return null; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Return the XY coordinates in a bounded grid of the cell containing a specific point. |
|
99 | - * |
|
100 | - * @param Point $point Point to find |
|
101 | - * @param Point $grid_NE North-East point of the bounded grid |
|
102 | - * @param Point $grid_SW South-West point fo the bounded grid |
|
103 | - * @param int $grid_columns Number of columns/rows in the grid |
|
104 | - * @return int[]|NULL |
|
105 | - */ |
|
106 | - protected function getGridCell(Point $point, Point $grid_NE, Point $grid_SW, int $grid_columns): ?array |
|
107 | - { |
|
108 | - list($x, $y) = $point->toArray(); |
|
109 | - list($x_max, $y_max) = $grid_NE->toArray(); |
|
110 | - list($x_min, $y_min) = $grid_SW->toArray(); |
|
111 | - |
|
112 | - $x_step = ($x_max - $x_min) / $grid_columns; |
|
113 | - $y_step = ($y_max - $y_min) / $grid_columns; |
|
114 | - |
|
115 | - if ($x_min <= $x && $x <= $x_max && $y_min <= $y && $y <= $y_max) { |
|
116 | - return [ |
|
117 | - $x === $x_max ? $grid_columns - 1 : intval(($x - $x_min) / $x_step), |
|
118 | - $y === $y_max ? $grid_columns - 1 : intval(($y - $y_min) / $y_step) |
|
119 | - ]; |
|
120 | - } |
|
121 | - return null; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Get an indexed array of the features of the map. |
|
126 | - * |
|
127 | - * {@internal The map is divided in a grid, eacg cell containing the features which bounding box overlaps that cell. |
|
128 | - * The grid is computed once for each map, and cached.} |
|
129 | - * |
|
130 | - * @phpcs:ignore Generic.Files.LineLength.TooLong |
|
131 | - * @return array{grid: array<int, array<int, \Brick\Geo\IO\GeoJSON\Feature[]>>, nb_columns: int, map_NE: \Brick\Geo\Point, map_SW: \Brick\Geo\Point, SRID: int}|NULL |
|
132 | - */ |
|
133 | - protected function featuresIndex(): ?array |
|
134 | - { |
|
135 | - $cacheKey = $this->cacheKey(); |
|
136 | - if ($cacheKey === null) { |
|
137 | - return null; |
|
138 | - } |
|
139 | - return Registry::cache()->array()->remember($cacheKey, function (): ?array { |
|
140 | - $map_def = $this->data('map'); |
|
141 | - if ( |
|
142 | - !$this->setGeometryEngine() |
|
143 | - || $map_def === null |
|
144 | - || !($map_def instanceof MapDefinitionInterface) |
|
145 | - ) { |
|
146 | - return null; |
|
147 | - } |
|
148 | - $bounding_boxes = []; |
|
149 | - $map_bounding_box = new BoundingBox(); |
|
150 | - $srid = 0; |
|
151 | - foreach ($map_def->features() as $feature) { |
|
152 | - $geometry = $feature->getGeometry(); |
|
153 | - if ($geometry === null) { |
|
154 | - continue; |
|
155 | - } |
|
156 | - $srid = $geometry->SRID(); |
|
157 | - $bounding_box = $geometry->getBoundingBox(); |
|
158 | - $bounding_boxes[] = [$feature, $bounding_box]; |
|
159 | - $map_bounding_box = $map_bounding_box->extendedWithBoundingBox($bounding_box); |
|
160 | - } |
|
161 | - $grid_columns = count($bounding_boxes); |
|
162 | - $grid = array_fill(0, $grid_columns, array_fill(0, $grid_columns, [])); |
|
163 | - $map_NE = $map_bounding_box->getNorthEast(); |
|
164 | - $map_SW = $map_bounding_box->getSouthWest(); |
|
165 | - foreach ($bounding_boxes as $item) { |
|
166 | - $grid_box_SW = $this->getGridCell($item[1]->getSouthWest(), $map_NE, $map_SW, $grid_columns) ?? [1, 1]; |
|
167 | - $grid_box_NE = $this->getGridCell($item[1]->getNorthEast(), $map_NE, $map_SW, $grid_columns) ?? [0, 0]; |
|
168 | - for ($i = $grid_box_SW[0]; $i <= $grid_box_NE[0]; $i++) { |
|
169 | - for ($j = $grid_box_SW[1]; $j <= $grid_box_NE[1]; $j++) { |
|
170 | - $grid[$i][$j][] = $item[0]; |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
174 | - return [ |
|
175 | - 'grid' => $grid, |
|
176 | - 'nb_columns' => $grid_columns, |
|
177 | - 'map_NE' => $map_NE, |
|
178 | - 'map_SW' => $map_SW, |
|
179 | - 'SRID' => $srid |
|
180 | - ]; |
|
181 | - }); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Set the Brick Geo Engine to use the database for geospatial computations. |
|
186 | - * The engine is set only if it has not been set beforehand. |
|
187 | - * |
|
188 | - * @return bool |
|
189 | - */ |
|
190 | - protected function setGeometryEngine(): bool |
|
191 | - { |
|
192 | - try { |
|
193 | - if (!GeometryEngineRegistry::has()) { |
|
194 | - GeometryEngineRegistry::set(new PDOEngine(DB::connection()->getPdo())); |
|
195 | - } |
|
196 | - $point = Point::xy(1, 1); |
|
197 | - return $point->equals($point); |
|
198 | - } catch (Throwable $ex) { |
|
199 | - } |
|
200 | - return false; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Get the key to cache the indexed grid of features. |
|
205 | - * |
|
206 | - * @return string|NULL |
|
207 | - */ |
|
208 | - protected function cacheKey(): ?string |
|
209 | - { |
|
210 | - if ($this->cache_key === null) { |
|
211 | - $map_def = $this->data('map'); |
|
212 | - if ($map_def === null || !($map_def instanceof MapDefinitionInterface)) { |
|
213 | - return null; |
|
214 | - } |
|
215 | - return spl_object_id($this) . '-map-' . $map_def->id(); |
|
216 | - } |
|
217 | - return $this->cache_key; |
|
218 | - } |
|
39 | + use PlaceMapperTrait; |
|
40 | + |
|
41 | + private ?string $cache_key = null; |
|
42 | + |
|
43 | + /** |
|
44 | + * {@inheritDoc} |
|
45 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::title() |
|
46 | + */ |
|
47 | + public function title(): string |
|
48 | + { |
|
49 | + return I18N::translate('Mapping on place coordinates'); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * {@inheritDoc} |
|
54 | + * |
|
55 | + * {@internal The Place is associated to a Point only. |
|
56 | + * PlaceLocation can calculate a BoundingBox. |
|
57 | + * Using a BoundingBox could make the mapping more complex and potentially arbitary. |
|
58 | + * Furthermore, when no coordinate is found for the place or its children, then it bubbles up to the parents. |
|
59 | + * This could create the unwanted side effect of a very large area to consider} |
|
60 | + * |
|
61 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperInterface::map() |
|
62 | + */ |
|
63 | + public function map(Place $place, string $feature_property): ?string |
|
64 | + { |
|
65 | + $location = new PlaceLocation($place->gedcomName()); |
|
66 | + $longitude = $location->longitude(); |
|
67 | + $latitude = $location->latitude(); |
|
68 | + if ($longitude === null || $latitude === null) { |
|
69 | + return null; |
|
70 | + } |
|
71 | + |
|
72 | + $features_index = $this->featuresIndex(); |
|
73 | + if ($features_index === null) { |
|
74 | + return null; |
|
75 | + } |
|
76 | + |
|
77 | + $place_point = Point::xy($longitude, $latitude, $features_index['SRID']); |
|
78 | + $grid_box = $this->getGridCell( |
|
79 | + $place_point, |
|
80 | + $features_index['map_NE'], |
|
81 | + $features_index['map_SW'], |
|
82 | + $features_index['nb_columns'] |
|
83 | + ); |
|
84 | + if ($grid_box === null || !$this->setGeometryEngine()) { |
|
85 | + return null; |
|
86 | + } |
|
87 | + $features = $features_index['grid'][$grid_box[0]][$grid_box[1]]; |
|
88 | + foreach ($features as $feature) { |
|
89 | + $geometry = $feature->getGeometry(); |
|
90 | + if ($geometry !== null && $place_point->SRID() === $geometry->SRID() && $geometry->contains($place_point)) { |
|
91 | + return $feature->getProperty($feature_property); |
|
92 | + } |
|
93 | + } |
|
94 | + return null; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Return the XY coordinates in a bounded grid of the cell containing a specific point. |
|
99 | + * |
|
100 | + * @param Point $point Point to find |
|
101 | + * @param Point $grid_NE North-East point of the bounded grid |
|
102 | + * @param Point $grid_SW South-West point fo the bounded grid |
|
103 | + * @param int $grid_columns Number of columns/rows in the grid |
|
104 | + * @return int[]|NULL |
|
105 | + */ |
|
106 | + protected function getGridCell(Point $point, Point $grid_NE, Point $grid_SW, int $grid_columns): ?array |
|
107 | + { |
|
108 | + list($x, $y) = $point->toArray(); |
|
109 | + list($x_max, $y_max) = $grid_NE->toArray(); |
|
110 | + list($x_min, $y_min) = $grid_SW->toArray(); |
|
111 | + |
|
112 | + $x_step = ($x_max - $x_min) / $grid_columns; |
|
113 | + $y_step = ($y_max - $y_min) / $grid_columns; |
|
114 | + |
|
115 | + if ($x_min <= $x && $x <= $x_max && $y_min <= $y && $y <= $y_max) { |
|
116 | + return [ |
|
117 | + $x === $x_max ? $grid_columns - 1 : intval(($x - $x_min) / $x_step), |
|
118 | + $y === $y_max ? $grid_columns - 1 : intval(($y - $y_min) / $y_step) |
|
119 | + ]; |
|
120 | + } |
|
121 | + return null; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Get an indexed array of the features of the map. |
|
126 | + * |
|
127 | + * {@internal The map is divided in a grid, eacg cell containing the features which bounding box overlaps that cell. |
|
128 | + * The grid is computed once for each map, and cached.} |
|
129 | + * |
|
130 | + * @phpcs:ignore Generic.Files.LineLength.TooLong |
|
131 | + * @return array{grid: array<int, array<int, \Brick\Geo\IO\GeoJSON\Feature[]>>, nb_columns: int, map_NE: \Brick\Geo\Point, map_SW: \Brick\Geo\Point, SRID: int}|NULL |
|
132 | + */ |
|
133 | + protected function featuresIndex(): ?array |
|
134 | + { |
|
135 | + $cacheKey = $this->cacheKey(); |
|
136 | + if ($cacheKey === null) { |
|
137 | + return null; |
|
138 | + } |
|
139 | + return Registry::cache()->array()->remember($cacheKey, function (): ?array { |
|
140 | + $map_def = $this->data('map'); |
|
141 | + if ( |
|
142 | + !$this->setGeometryEngine() |
|
143 | + || $map_def === null |
|
144 | + || !($map_def instanceof MapDefinitionInterface) |
|
145 | + ) { |
|
146 | + return null; |
|
147 | + } |
|
148 | + $bounding_boxes = []; |
|
149 | + $map_bounding_box = new BoundingBox(); |
|
150 | + $srid = 0; |
|
151 | + foreach ($map_def->features() as $feature) { |
|
152 | + $geometry = $feature->getGeometry(); |
|
153 | + if ($geometry === null) { |
|
154 | + continue; |
|
155 | + } |
|
156 | + $srid = $geometry->SRID(); |
|
157 | + $bounding_box = $geometry->getBoundingBox(); |
|
158 | + $bounding_boxes[] = [$feature, $bounding_box]; |
|
159 | + $map_bounding_box = $map_bounding_box->extendedWithBoundingBox($bounding_box); |
|
160 | + } |
|
161 | + $grid_columns = count($bounding_boxes); |
|
162 | + $grid = array_fill(0, $grid_columns, array_fill(0, $grid_columns, [])); |
|
163 | + $map_NE = $map_bounding_box->getNorthEast(); |
|
164 | + $map_SW = $map_bounding_box->getSouthWest(); |
|
165 | + foreach ($bounding_boxes as $item) { |
|
166 | + $grid_box_SW = $this->getGridCell($item[1]->getSouthWest(), $map_NE, $map_SW, $grid_columns) ?? [1, 1]; |
|
167 | + $grid_box_NE = $this->getGridCell($item[1]->getNorthEast(), $map_NE, $map_SW, $grid_columns) ?? [0, 0]; |
|
168 | + for ($i = $grid_box_SW[0]; $i <= $grid_box_NE[0]; $i++) { |
|
169 | + for ($j = $grid_box_SW[1]; $j <= $grid_box_NE[1]; $j++) { |
|
170 | + $grid[$i][$j][] = $item[0]; |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | + return [ |
|
175 | + 'grid' => $grid, |
|
176 | + 'nb_columns' => $grid_columns, |
|
177 | + 'map_NE' => $map_NE, |
|
178 | + 'map_SW' => $map_SW, |
|
179 | + 'SRID' => $srid |
|
180 | + ]; |
|
181 | + }); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Set the Brick Geo Engine to use the database for geospatial computations. |
|
186 | + * The engine is set only if it has not been set beforehand. |
|
187 | + * |
|
188 | + * @return bool |
|
189 | + */ |
|
190 | + protected function setGeometryEngine(): bool |
|
191 | + { |
|
192 | + try { |
|
193 | + if (!GeometryEngineRegistry::has()) { |
|
194 | + GeometryEngineRegistry::set(new PDOEngine(DB::connection()->getPdo())); |
|
195 | + } |
|
196 | + $point = Point::xy(1, 1); |
|
197 | + return $point->equals($point); |
|
198 | + } catch (Throwable $ex) { |
|
199 | + } |
|
200 | + return false; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Get the key to cache the indexed grid of features. |
|
205 | + * |
|
206 | + * @return string|NULL |
|
207 | + */ |
|
208 | + protected function cacheKey(): ?string |
|
209 | + { |
|
210 | + if ($this->cache_key === null) { |
|
211 | + $map_def = $this->data('map'); |
|
212 | + if ($map_def === null || !($map_def instanceof MapDefinitionInterface)) { |
|
213 | + return null; |
|
214 | + } |
|
215 | + return spl_object_id($this) . '-map-' . $map_def->id(); |
|
216 | + } |
|
217 | + return $this->cache_key; |
|
218 | + } |
|
219 | 219 | } |
@@ -23,104 +23,104 @@ |
||
23 | 23 | */ |
24 | 24 | class GeoAnalysisResults |
25 | 25 | { |
26 | - private GeoAnalysisResult $global; |
|
26 | + private GeoAnalysisResult $global; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @var Collection<string, GeoAnalysisResult> $detailed |
|
30 | - */ |
|
31 | - private Collection $detailed; |
|
28 | + /** |
|
29 | + * @var Collection<string, GeoAnalysisResult> $detailed |
|
30 | + */ |
|
31 | + private Collection $detailed; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Constructor for GeoAnalysisResults |
|
35 | - */ |
|
36 | - public function __construct() |
|
37 | - { |
|
38 | - $this->global = new GeoAnalysisResult('Global', 0); |
|
39 | - $this->detailed = new Collection(); |
|
40 | - } |
|
33 | + /** |
|
34 | + * Constructor for GeoAnalysisResults |
|
35 | + */ |
|
36 | + public function __construct() |
|
37 | + { |
|
38 | + $this->global = new GeoAnalysisResult('Global', 0); |
|
39 | + $this->detailed = new Collection(); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Global result of the geographical analysis |
|
44 | - * |
|
45 | - * @return GeoAnalysisResult |
|
46 | - */ |
|
47 | - public function global(): GeoAnalysisResult |
|
48 | - { |
|
49 | - return $this->global; |
|
50 | - } |
|
42 | + /** |
|
43 | + * Global result of the geographical analysis |
|
44 | + * |
|
45 | + * @return GeoAnalysisResult |
|
46 | + */ |
|
47 | + public function global(): GeoAnalysisResult |
|
48 | + { |
|
49 | + return $this->global; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * List of results by category of the geographical analysis |
|
54 | - * |
|
55 | - * @return Collection<string, GeoAnalysisResult> |
|
56 | - */ |
|
57 | - public function detailed(): Collection |
|
58 | - { |
|
59 | - return $this->detailed; |
|
60 | - } |
|
52 | + /** |
|
53 | + * List of results by category of the geographical analysis |
|
54 | + * |
|
55 | + * @return Collection<string, GeoAnalysisResult> |
|
56 | + */ |
|
57 | + public function detailed(): Collection |
|
58 | + { |
|
59 | + return $this->detailed; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * List of results by category of the geographical analysis. |
|
64 | - * The list is sorted first by the category order, then by the category description |
|
65 | - * |
|
66 | - * @return Collection<string, GeoAnalysisResult> |
|
67 | - */ |
|
68 | - public function sortedDetailed(): Collection |
|
69 | - { |
|
70 | - return $this->detailed->sortBy([ |
|
71 | - fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int => $a->order() <=> $b->order(), |
|
72 | - fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int => |
|
73 | - I18N::comparator()($a->description(), $b->description()) |
|
74 | - ]); |
|
75 | - } |
|
62 | + /** |
|
63 | + * List of results by category of the geographical analysis. |
|
64 | + * The list is sorted first by the category order, then by the category description |
|
65 | + * |
|
66 | + * @return Collection<string, GeoAnalysisResult> |
|
67 | + */ |
|
68 | + public function sortedDetailed(): Collection |
|
69 | + { |
|
70 | + return $this->detailed->sortBy([ |
|
71 | + fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int => $a->order() <=> $b->order(), |
|
72 | + fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int => |
|
73 | + I18N::comparator()($a->description(), $b->description()) |
|
74 | + ]); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Add a GeoAnalysis Place to the global result |
|
79 | - * |
|
80 | - * @param GeoAnalysisPlace $place |
|
81 | - */ |
|
82 | - public function addPlace(GeoAnalysisPlace $place): void |
|
83 | - { |
|
84 | - $this->global()->addPlace($place); |
|
85 | - } |
|
77 | + /** |
|
78 | + * Add a GeoAnalysis Place to the global result |
|
79 | + * |
|
80 | + * @param GeoAnalysisPlace $place |
|
81 | + */ |
|
82 | + public function addPlace(GeoAnalysisPlace $place): void |
|
83 | + { |
|
84 | + $this->global()->addPlace($place); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Add a new category to the list of results, if it does not exist yet |
|
89 | - * |
|
90 | - * @param string $description |
|
91 | - * @param int $order |
|
92 | - */ |
|
93 | - public function addCategory(string $description, int $order): void |
|
94 | - { |
|
95 | - if (!$this->detailed->has($description)) { |
|
96 | - $this->detailed->put($description, new GeoAnalysisResult($description, $order)); |
|
97 | - } |
|
98 | - } |
|
87 | + /** |
|
88 | + * Add a new category to the list of results, if it does not exist yet |
|
89 | + * |
|
90 | + * @param string $description |
|
91 | + * @param int $order |
|
92 | + */ |
|
93 | + public function addCategory(string $description, int $order): void |
|
94 | + { |
|
95 | + if (!$this->detailed->has($description)) { |
|
96 | + $this->detailed->put($description, new GeoAnalysisResult($description, $order)); |
|
97 | + } |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Add a GeoAnalysis Place to a category result, if the category exist. |
|
102 | - * |
|
103 | - * @param string $category_name |
|
104 | - * @param GeoAnalysisPlace $place |
|
105 | - */ |
|
106 | - public function addPlaceInCreatedCategory(string $category_name, GeoAnalysisPlace $place): void |
|
107 | - { |
|
108 | - if ($this->detailed->has($category_name)) { |
|
109 | - $this->detailed->get($category_name)->addPlace($place); |
|
110 | - } |
|
111 | - } |
|
100 | + /** |
|
101 | + * Add a GeoAnalysis Place to a category result, if the category exist. |
|
102 | + * |
|
103 | + * @param string $category_name |
|
104 | + * @param GeoAnalysisPlace $place |
|
105 | + */ |
|
106 | + public function addPlaceInCreatedCategory(string $category_name, GeoAnalysisPlace $place): void |
|
107 | + { |
|
108 | + if ($this->detailed->has($category_name)) { |
|
109 | + $this->detailed->get($category_name)->addPlace($place); |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Add a GeoAnalysis Place to a category result, after creating the category if it does not exist. |
|
115 | - * |
|
116 | - * @param string $category_name |
|
117 | - * @param GeoAnalysisPlace $place |
|
118 | - */ |
|
119 | - public function addPlaceInCategory(string $category_name, int $category_order, GeoAnalysisPlace $place): void |
|
120 | - { |
|
121 | - if (!$this->detailed->has($category_name)) { |
|
122 | - $this->addCategory($category_name, $category_order); |
|
123 | - } |
|
124 | - $this->addPlaceInCreatedCategory($category_name, $place); |
|
125 | - } |
|
113 | + /** |
|
114 | + * Add a GeoAnalysis Place to a category result, after creating the category if it does not exist. |
|
115 | + * |
|
116 | + * @param string $category_name |
|
117 | + * @param GeoAnalysisPlace $place |
|
118 | + */ |
|
119 | + public function addPlaceInCategory(string $category_name, int $category_order, GeoAnalysisPlace $place): void |
|
120 | + { |
|
121 | + if (!$this->detailed->has($category_name)) { |
|
122 | + $this->addCategory($category_name, $category_order); |
|
123 | + } |
|
124 | + $this->addPlaceInCreatedCategory($category_name, $place); |
|
125 | + } |
|
126 | 126 | } |
@@ -25,105 +25,105 @@ |
||
25 | 25 | */ |
26 | 26 | class GenericPlaceMapperConfig implements PlaceMapperConfigInterface |
27 | 27 | { |
28 | - /** @var array<string, mixed> $config */ |
|
29 | - private array $config = []; |
|
28 | + /** @var array<string, mixed> $config */ |
|
29 | + private array $config = []; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Get the generic mapper's config |
|
33 | - * |
|
34 | - * @return array<string, mixed> |
|
35 | - */ |
|
36 | - public function config(): array |
|
37 | - { |
|
38 | - return $this->config; |
|
39 | - } |
|
31 | + /** |
|
32 | + * Get the generic mapper's config |
|
33 | + * |
|
34 | + * @return array<string, mixed> |
|
35 | + */ |
|
36 | + public function config(): array |
|
37 | + { |
|
38 | + return $this->config; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Set the generic mapper's config |
|
43 | - * |
|
44 | - * @param array<string, mixed> $config |
|
45 | - * @return $this |
|
46 | - */ |
|
47 | - public function setConfig(array $config): self |
|
48 | - { |
|
49 | - $this->config = $config; |
|
50 | - return $this; |
|
51 | - } |
|
41 | + /** |
|
42 | + * Set the generic mapper's config |
|
43 | + * |
|
44 | + * @param array<string, mixed> $config |
|
45 | + * @return $this |
|
46 | + */ |
|
47 | + public function setConfig(array $config): self |
|
48 | + { |
|
49 | + $this->config = $config; |
|
50 | + return $this; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * {@inheritDoc} |
|
55 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::get() |
|
56 | - */ |
|
57 | - public function get(string $key, $default = null) |
|
58 | - { |
|
59 | - return $this->config[$key] ?? $default; |
|
60 | - } |
|
53 | + /** |
|
54 | + * {@inheritDoc} |
|
55 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::get() |
|
56 | + */ |
|
57 | + public function get(string $key, $default = null) |
|
58 | + { |
|
59 | + return $this->config[$key] ?? $default; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritDoc} |
|
64 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::has() |
|
65 | - */ |
|
66 | - public function has(string $key): bool |
|
67 | - { |
|
68 | - return key_exists($key, $this->config); |
|
69 | - } |
|
62 | + /** |
|
63 | + * {@inheritDoc} |
|
64 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::has() |
|
65 | + */ |
|
66 | + public function has(string $key): bool |
|
67 | + { |
|
68 | + return key_exists($key, $this->config); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * {@inheritDoc} |
|
73 | - * @see \JsonSerializable::jsonSerialize() |
|
74 | - */ |
|
75 | - public function jsonSerialize() |
|
76 | - { |
|
77 | - return [ |
|
78 | - 'class' => get_class($this), |
|
79 | - 'config' => $this->jsonSerializeConfig() |
|
80 | - ]; |
|
81 | - } |
|
71 | + /** |
|
72 | + * {@inheritDoc} |
|
73 | + * @see \JsonSerializable::jsonSerialize() |
|
74 | + */ |
|
75 | + public function jsonSerialize() |
|
76 | + { |
|
77 | + return [ |
|
78 | + 'class' => get_class($this), |
|
79 | + 'config' => $this->jsonSerializeConfig() |
|
80 | + ]; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Returns a representation of the mapper config compatible with Json serialisation |
|
85 | - * |
|
86 | - * @return mixed |
|
87 | - */ |
|
88 | - public function jsonSerializeConfig() |
|
89 | - { |
|
90 | - return $this->config; |
|
91 | - } |
|
83 | + /** |
|
84 | + * Returns a representation of the mapper config compatible with Json serialisation |
|
85 | + * |
|
86 | + * @return mixed |
|
87 | + */ |
|
88 | + public function jsonSerializeConfig() |
|
89 | + { |
|
90 | + return $this->config; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * {@inheritDoc} |
|
95 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::jsonDeserialize() |
|
96 | - * |
|
97 | - * @param mixed $config |
|
98 | - * @return $this |
|
99 | - */ |
|
100 | - public function jsonDeserialize($config): self |
|
101 | - { |
|
102 | - if (is_string($config)) { |
|
103 | - return $this->jsonDeserialize(json_decode($config)); |
|
104 | - } |
|
105 | - if (is_array($config)) { |
|
106 | - return $this->setConfig($config); |
|
107 | - } |
|
108 | - return $this; |
|
109 | - } |
|
93 | + /** |
|
94 | + * {@inheritDoc} |
|
95 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::jsonDeserialize() |
|
96 | + * |
|
97 | + * @param mixed $config |
|
98 | + * @return $this |
|
99 | + */ |
|
100 | + public function jsonDeserialize($config): self |
|
101 | + { |
|
102 | + if (is_string($config)) { |
|
103 | + return $this->jsonDeserialize(json_decode($config)); |
|
104 | + } |
|
105 | + if (is_array($config)) { |
|
106 | + return $this->setConfig($config); |
|
107 | + } |
|
108 | + return $this; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * {@inheritDoc} |
|
113 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::configContent() |
|
114 | - */ |
|
115 | - public function configContent(ModuleInterface $module, Tree $tree): string |
|
116 | - { |
|
117 | - return ''; |
|
118 | - } |
|
111 | + /** |
|
112 | + * {@inheritDoc} |
|
113 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::configContent() |
|
114 | + */ |
|
115 | + public function configContent(ModuleInterface $module, Tree $tree): string |
|
116 | + { |
|
117 | + return ''; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * {@inheritDoc} |
|
122 | - * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::withConfigUpdate() |
|
123 | - * @return $this |
|
124 | - */ |
|
125 | - public function withConfigUpdate(ServerRequestInterface $request): self |
|
126 | - { |
|
127 | - return $this; |
|
128 | - } |
|
120 | + /** |
|
121 | + * {@inheritDoc} |
|
122 | + * @see \MyArtJaub\Webtrees\Contracts\GeoDispersion\PlaceMapperConfigInterface::withConfigUpdate() |
|
123 | + * @return $this |
|
124 | + */ |
|
125 | + public function withConfigUpdate(ServerRequestInterface $request): self |
|
126 | + { |
|
127 | + return $this; |
|
128 | + } |
|
129 | 129 | } |
@@ -22,51 +22,51 @@ |
||
22 | 22 | |
23 | 23 | class GeoAnalysisTable extends AbstractGeoAnalysisView |
24 | 24 | { |
25 | - /** |
|
26 | - * {@inheritDoc} |
|
27 | - * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::type() |
|
28 | - */ |
|
29 | - public function type(): string |
|
30 | - { |
|
31 | - return I18N::translateContext('GEODISPERSION', 'Table'); |
|
32 | - } |
|
25 | + /** |
|
26 | + * {@inheritDoc} |
|
27 | + * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::type() |
|
28 | + */ |
|
29 | + public function type(): string |
|
30 | + { |
|
31 | + return I18N::translateContext('GEODISPERSION', 'Table'); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritDoc} |
|
36 | - * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::icon() |
|
37 | - */ |
|
38 | - public function icon(ModuleInterface $module): string |
|
39 | - { |
|
40 | - return view($module->name() . '::icons/view-table', ['type' => $this->type()]); |
|
41 | - } |
|
34 | + /** |
|
35 | + * {@inheritDoc} |
|
36 | + * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::icon() |
|
37 | + */ |
|
38 | + public function icon(ModuleInterface $module): string |
|
39 | + { |
|
40 | + return view($module->name() . '::icons/view-table', ['type' => $this->type()]); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * {@inheritDoc} |
|
45 | - * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalSettingsContent() |
|
46 | - */ |
|
47 | - public function globalSettingsContent(ModuleInterface $module): string |
|
48 | - { |
|
49 | - return ''; |
|
50 | - } |
|
43 | + /** |
|
44 | + * {@inheritDoc} |
|
45 | + * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalSettingsContent() |
|
46 | + */ |
|
47 | + public function globalSettingsContent(ModuleInterface $module): string |
|
48 | + { |
|
49 | + return ''; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * {@inheritDoc} |
|
54 | - * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::withGlobalSettingsUpdate() |
|
55 | - * @return $this |
|
56 | - */ |
|
57 | - public function withGlobalSettingsUpdate(ServerRequestInterface $request): self |
|
58 | - { |
|
59 | - return $this; |
|
60 | - } |
|
52 | + /** |
|
53 | + * {@inheritDoc} |
|
54 | + * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::withGlobalSettingsUpdate() |
|
55 | + * @return $this |
|
56 | + */ |
|
57 | + public function withGlobalSettingsUpdate(ServerRequestInterface $request): self |
|
58 | + { |
|
59 | + return $this; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritDoc} |
|
64 | - * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalTabContent() |
|
65 | - */ |
|
66 | - public function globalTabContent(GeoDispersionModule $module, GeoAnalysisResult $result, array $params): string |
|
67 | - { |
|
68 | - return view($module->name() . '::geoanalysisview-tab-glb-table', $params + [ |
|
69 | - 'result' => $result |
|
70 | - ]); |
|
71 | - } |
|
62 | + /** |
|
63 | + * {@inheritDoc} |
|
64 | + * @see \MyArtJaub\Webtrees\Module\GeoDispersion\Views\AbstractGeoAnalysisView::globalTabContent() |
|
65 | + */ |
|
66 | + public function globalTabContent(GeoDispersionModule $module, GeoAnalysisResult $result, array $params): string |
|
67 | + { |
|
68 | + return view($module->name() . '::geoanalysisview-tab-glb-table', $params + [ |
|
69 | + 'result' => $result |
|
70 | + ]); |
|
71 | + } |
|
72 | 72 | } |
@@ -27,224 +27,224 @@ |
||
27 | 27 | */ |
28 | 28 | abstract class AbstractGeoAnalysisView |
29 | 29 | { |
30 | - private int $id; |
|
31 | - private Tree $tree; |
|
32 | - private bool $enabled; |
|
33 | - private string $description; |
|
34 | - private GeoAnalysisInterface $geoanalysis; |
|
35 | - private int $depth; |
|
36 | - private int $detailed_top_places; |
|
37 | - private bool $use_flags; |
|
38 | - |
|
39 | - /** |
|
40 | - * Constructor for AbstractGeoAnalysisView |
|
41 | - * |
|
42 | - * @param int $id |
|
43 | - * @param Tree $tree |
|
44 | - * @param bool $enabled |
|
45 | - * @param string $description |
|
46 | - * @param GeoAnalysisInterface $geoanalysis |
|
47 | - * @param int $depth |
|
48 | - * @param int $detailed_top_places |
|
49 | - * @param bool $use_flags |
|
50 | - */ |
|
51 | - final public function __construct( |
|
52 | - int $id, |
|
53 | - Tree $tree, |
|
54 | - bool $enabled, |
|
55 | - string $description, |
|
56 | - GeoAnalysisInterface $geoanalysis, |
|
57 | - int $depth, |
|
58 | - int $detailed_top_places = 0, |
|
59 | - bool $use_flags = false |
|
60 | - ) { |
|
61 | - $this->id = $id; |
|
62 | - $this->tree = $tree; |
|
63 | - $this->enabled = $enabled; |
|
64 | - $this->description = $description; |
|
65 | - $this->geoanalysis = $geoanalysis; |
|
66 | - $this->depth = $depth; |
|
67 | - $this->detailed_top_places = $detailed_top_places; |
|
68 | - $this->use_flags = $use_flags; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Create a copy of the view with a new ID. |
|
73 | - * |
|
74 | - * @param int $id |
|
75 | - * @return static |
|
76 | - */ |
|
77 | - public function withId(int $id): self |
|
78 | - { |
|
79 | - $new = clone $this; |
|
80 | - $new->id = $id; |
|
81 | - return $new; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Create a copy of the view with new properties. |
|
86 | - * |
|
87 | - * @param bool $enabled |
|
88 | - * @param string $description |
|
89 | - * @param GeoAnalysisInterface $geoanalysis |
|
90 | - * @param int $depth |
|
91 | - * @param int $detailed_top_places |
|
92 | - * @param bool $use_flags |
|
93 | - * @return static |
|
94 | - */ |
|
95 | - public function with( |
|
96 | - bool $enabled, |
|
97 | - string $description, |
|
98 | - GeoAnalysisInterface $geoanalysis, |
|
99 | - int $depth, |
|
100 | - int $detailed_top_places = 0, |
|
101 | - bool $use_flags = false |
|
102 | - ): self { |
|
103 | - $new = clone $this; |
|
104 | - $new->enabled = $enabled; |
|
105 | - $new->description = $description; |
|
106 | - $new->geoanalysis = $geoanalysis; |
|
107 | - $new->depth = $depth; |
|
108 | - $new->detailed_top_places = $detailed_top_places; |
|
109 | - $new->use_flags = $use_flags; |
|
110 | - return $new; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the view ID |
|
115 | - * |
|
116 | - * @return int |
|
117 | - */ |
|
118 | - public function id(): int |
|
119 | - { |
|
120 | - return $this->id; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Get the view type for display |
|
125 | - * |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - abstract public function type(): string; |
|
129 | - |
|
130 | - /** |
|
131 | - * Get the icon for the view type |
|
132 | - * |
|
133 | - * @param ModuleInterface $module |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - abstract public function icon(ModuleInterface $module): string; |
|
137 | - |
|
138 | - /** |
|
139 | - * Return the content of the global settings section of the config page |
|
140 | - * |
|
141 | - * @param ModuleInterface $module |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - abstract public function globalSettingsContent(ModuleInterface $module): string; |
|
145 | - |
|
146 | - /** |
|
147 | - * Return a view with global settings updated according to the view rules |
|
148 | - * |
|
149 | - * @param ServerRequestInterface $request |
|
150 | - * @return static |
|
151 | - */ |
|
152 | - abstract public function withGlobalSettingsUpdate(ServerRequestInterface $request): self; |
|
153 | - |
|
154 | - /** |
|
155 | - * Returns the content of the view global tab |
|
156 | - * |
|
157 | - * @param GeoDispersionModule $module |
|
158 | - * @param GeoAnalysisResult $result |
|
159 | - * @param array<string, mixed> $params |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - abstract public function globalTabContent( |
|
163 | - GeoDispersionModule $module, |
|
164 | - GeoAnalysisResult $result, |
|
165 | - array $params |
|
166 | - ): string; |
|
167 | - |
|
168 | - /** |
|
169 | - * Returns the content of the view detailed tab |
|
170 | - * |
|
171 | - * @param ModuleInterface $module |
|
172 | - * @param Collection<string, GeoAnalysisResult> $results |
|
173 | - * @param array<string, mixed> $params |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public function detailedTabContent(ModuleInterface $module, Collection $results, array $params): string |
|
177 | - { |
|
178 | - return view($module->name() . '::geoanalysisview-tab-detailed', $params + [ 'results' => $results ]); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the tree to which the view belongs |
|
183 | - * |
|
184 | - * @return Tree |
|
185 | - */ |
|
186 | - public function tree(): Tree |
|
187 | - { |
|
188 | - return $this->tree; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get the description of the view |
|
193 | - * |
|
194 | - * @return string |
|
195 | - */ |
|
196 | - public function description(): string |
|
197 | - { |
|
198 | - return $this->description; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Get whether the view is enabled |
|
203 | - * |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function isEnabled(): bool |
|
207 | - { |
|
208 | - return $this->enabled; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get the geographical dispersion analysis for the view |
|
213 | - * |
|
214 | - * @return GeoAnalysisInterface |
|
215 | - */ |
|
216 | - public function analysis(): GeoAnalysisInterface |
|
217 | - { |
|
218 | - return $this->geoanalysis; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get the place hierarchy depth for the view |
|
223 | - * |
|
224 | - * @return int |
|
225 | - */ |
|
226 | - public function placesDepth(): int |
|
227 | - { |
|
228 | - return $this->depth; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get the number of places to display in the detailed tab |
|
233 | - * |
|
234 | - * @return int |
|
235 | - */ |
|
236 | - public function numberTopPlaces(): int |
|
237 | - { |
|
238 | - return $this->detailed_top_places; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Get whether flags should be used in the detailed tab |
|
243 | - * |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function useFlags(): bool |
|
247 | - { |
|
248 | - return $this->use_flags; |
|
249 | - } |
|
30 | + private int $id; |
|
31 | + private Tree $tree; |
|
32 | + private bool $enabled; |
|
33 | + private string $description; |
|
34 | + private GeoAnalysisInterface $geoanalysis; |
|
35 | + private int $depth; |
|
36 | + private int $detailed_top_places; |
|
37 | + private bool $use_flags; |
|
38 | + |
|
39 | + /** |
|
40 | + * Constructor for AbstractGeoAnalysisView |
|
41 | + * |
|
42 | + * @param int $id |
|
43 | + * @param Tree $tree |
|
44 | + * @param bool $enabled |
|
45 | + * @param string $description |
|
46 | + * @param GeoAnalysisInterface $geoanalysis |
|
47 | + * @param int $depth |
|
48 | + * @param int $detailed_top_places |
|
49 | + * @param bool $use_flags |
|
50 | + */ |
|
51 | + final public function __construct( |
|
52 | + int $id, |
|
53 | + Tree $tree, |
|
54 | + bool $enabled, |
|
55 | + string $description, |
|
56 | + GeoAnalysisInterface $geoanalysis, |
|
57 | + int $depth, |
|
58 | + int $detailed_top_places = 0, |
|
59 | + bool $use_flags = false |
|
60 | + ) { |
|
61 | + $this->id = $id; |
|
62 | + $this->tree = $tree; |
|
63 | + $this->enabled = $enabled; |
|
64 | + $this->description = $description; |
|
65 | + $this->geoanalysis = $geoanalysis; |
|
66 | + $this->depth = $depth; |
|
67 | + $this->detailed_top_places = $detailed_top_places; |
|
68 | + $this->use_flags = $use_flags; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Create a copy of the view with a new ID. |
|
73 | + * |
|
74 | + * @param int $id |
|
75 | + * @return static |
|
76 | + */ |
|
77 | + public function withId(int $id): self |
|
78 | + { |
|
79 | + $new = clone $this; |
|
80 | + $new->id = $id; |
|
81 | + return $new; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Create a copy of the view with new properties. |
|
86 | + * |
|
87 | + * @param bool $enabled |
|
88 | + * @param string $description |
|
89 | + * @param GeoAnalysisInterface $geoanalysis |
|
90 | + * @param int $depth |
|
91 | + * @param int $detailed_top_places |
|
92 | + * @param bool $use_flags |
|
93 | + * @return static |
|
94 | + */ |
|
95 | + public function with( |
|
96 | + bool $enabled, |
|
97 | + string $description, |
|
98 | + GeoAnalysisInterface $geoanalysis, |
|
99 | + int $depth, |
|
100 | + int $detailed_top_places = 0, |
|
101 | + bool $use_flags = false |
|
102 | + ): self { |
|
103 | + $new = clone $this; |
|
104 | + $new->enabled = $enabled; |
|
105 | + $new->description = $description; |
|
106 | + $new->geoanalysis = $geoanalysis; |
|
107 | + $new->depth = $depth; |
|
108 | + $new->detailed_top_places = $detailed_top_places; |
|
109 | + $new->use_flags = $use_flags; |
|
110 | + return $new; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the view ID |
|
115 | + * |
|
116 | + * @return int |
|
117 | + */ |
|
118 | + public function id(): int |
|
119 | + { |
|
120 | + return $this->id; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Get the view type for display |
|
125 | + * |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + abstract public function type(): string; |
|
129 | + |
|
130 | + /** |
|
131 | + * Get the icon for the view type |
|
132 | + * |
|
133 | + * @param ModuleInterface $module |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + abstract public function icon(ModuleInterface $module): string; |
|
137 | + |
|
138 | + /** |
|
139 | + * Return the content of the global settings section of the config page |
|
140 | + * |
|
141 | + * @param ModuleInterface $module |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + abstract public function globalSettingsContent(ModuleInterface $module): string; |
|
145 | + |
|
146 | + /** |
|
147 | + * Return a view with global settings updated according to the view rules |
|
148 | + * |
|
149 | + * @param ServerRequestInterface $request |
|
150 | + * @return static |
|
151 | + */ |
|
152 | + abstract public function withGlobalSettingsUpdate(ServerRequestInterface $request): self; |
|
153 | + |
|
154 | + /** |
|
155 | + * Returns the content of the view global tab |
|
156 | + * |
|
157 | + * @param GeoDispersionModule $module |
|
158 | + * @param GeoAnalysisResult $result |
|
159 | + * @param array<string, mixed> $params |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + abstract public function globalTabContent( |
|
163 | + GeoDispersionModule $module, |
|
164 | + GeoAnalysisResult $result, |
|
165 | + array $params |
|
166 | + ): string; |
|
167 | + |
|
168 | + /** |
|
169 | + * Returns the content of the view detailed tab |
|
170 | + * |
|
171 | + * @param ModuleInterface $module |
|
172 | + * @param Collection<string, GeoAnalysisResult> $results |
|
173 | + * @param array<string, mixed> $params |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public function detailedTabContent(ModuleInterface $module, Collection $results, array $params): string |
|
177 | + { |
|
178 | + return view($module->name() . '::geoanalysisview-tab-detailed', $params + [ 'results' => $results ]); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the tree to which the view belongs |
|
183 | + * |
|
184 | + * @return Tree |
|
185 | + */ |
|
186 | + public function tree(): Tree |
|
187 | + { |
|
188 | + return $this->tree; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get the description of the view |
|
193 | + * |
|
194 | + * @return string |
|
195 | + */ |
|
196 | + public function description(): string |
|
197 | + { |
|
198 | + return $this->description; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Get whether the view is enabled |
|
203 | + * |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function isEnabled(): bool |
|
207 | + { |
|
208 | + return $this->enabled; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get the geographical dispersion analysis for the view |
|
213 | + * |
|
214 | + * @return GeoAnalysisInterface |
|
215 | + */ |
|
216 | + public function analysis(): GeoAnalysisInterface |
|
217 | + { |
|
218 | + return $this->geoanalysis; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get the place hierarchy depth for the view |
|
223 | + * |
|
224 | + * @return int |
|
225 | + */ |
|
226 | + public function placesDepth(): int |
|
227 | + { |
|
228 | + return $this->depth; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get the number of places to display in the detailed tab |
|
233 | + * |
|
234 | + * @return int |
|
235 | + */ |
|
236 | + public function numberTopPlaces(): int |
|
237 | + { |
|
238 | + return $this->detailed_top_places; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Get whether flags should be used in the detailed tab |
|
243 | + * |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function useFlags(): bool |
|
247 | + { |
|
248 | + return $this->use_flags; |
|
249 | + } |
|
250 | 250 | } |
@@ -29,514 +29,514 @@ |
||
29 | 29 | */ |
30 | 30 | class SosaStatisticsService |
31 | 31 | { |
32 | - private UserInterface $user; |
|
33 | - private Tree $tree; |
|
34 | - |
|
35 | - /** |
|
36 | - * Constructor for Sosa Statistics Service |
|
37 | - * |
|
38 | - * @param Tree $tree |
|
39 | - * @param UserInterface $user |
|
40 | - */ |
|
41 | - public function __construct(Tree $tree, UserInterface $user) |
|
42 | - { |
|
43 | - $this->tree = $tree; |
|
44 | - $this->user = $user; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Return the root individual for the reference tree and user. |
|
49 | - * |
|
50 | - * @return Individual|NULL |
|
51 | - */ |
|
52 | - public function rootIndividual(): ?Individual |
|
53 | - { |
|
54 | - $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
55 | - return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Get the highest generation for the reference tree and user. |
|
60 | - * |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function maxGeneration(): int |
|
64 | - { |
|
65 | - return (int) DB::table('maj_sosa') |
|
66 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
67 | - ->where('majs_user_id', '=', $this->user->id()) |
|
68 | - ->max('majs_gen'); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get the total count of individuals in the tree. |
|
73 | - * |
|
74 | - * @return int |
|
75 | - */ |
|
76 | - public function totalIndividuals(): int |
|
77 | - { |
|
78 | - return DB::table('individuals') |
|
79 | - ->where('i_file', '=', $this->tree->id()) |
|
80 | - ->count(); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get the total count of Sosa ancestors for all generations |
|
85 | - * |
|
86 | - * @return int |
|
87 | - */ |
|
88 | - public function totalAncestors(): int |
|
89 | - { |
|
90 | - return DB::table('maj_sosa') |
|
91 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
92 | - ->where('majs_user_id', '=', $this->user->id()) |
|
93 | - ->count(); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get the total count of Sosa ancestors for a generation |
|
98 | - * |
|
99 | - * @return int |
|
100 | - */ |
|
101 | - public function totalAncestorsAtGeneration(int $gen): int |
|
102 | - { |
|
103 | - return DB::table('maj_sosa') |
|
104 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
105 | - ->where('majs_user_id', '=', $this->user->id()) |
|
106 | - ->where('majs_gen', '=', $gen) |
|
107 | - ->count(); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get the total count of distinct Sosa ancestors for all generations |
|
112 | - * |
|
113 | - * @return int |
|
114 | - */ |
|
115 | - public function totalDistinctAncestors(): int |
|
116 | - { |
|
117 | - return DB::table('maj_sosa') |
|
118 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
119 | - ->where('majs_user_id', '=', $this->user->id()) |
|
120 | - ->distinct() |
|
121 | - ->count('majs_i_id'); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
126 | - * |
|
127 | - * @return float |
|
128 | - */ |
|
129 | - public function meanGenerationTime(): float |
|
130 | - { |
|
131 | - $row = DB::table('maj_sosa') |
|
132 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
133 | - ->where('majs_user_id', '=', $this->user->id()) |
|
134 | - ->whereNotNull('majs_birth_year') |
|
135 | - ->selectRaw('COUNT(majs_sosa) AS n') |
|
136 | - ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
137 | - ->selectRaw('SUM(majs_gen) AS sum_x') |
|
138 | - ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
139 | - ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
140 | - ->get()->first(); |
|
141 | - |
|
142 | - return $row->n == 0 ? 0 : |
|
143 | - -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get the statistic array detailed by generation. |
|
148 | - * Statistics for each generation are: |
|
149 | - * - The number of Sosa in generation |
|
150 | - * - The number of Sosa up to generation |
|
151 | - * - The number of distinct Sosa up to generation |
|
152 | - * - The year of the first birth in generation |
|
153 | - * - The year of the first estimated birth in generation |
|
154 | - * - The year of the last birth in generation |
|
155 | - * - The year of the last estimated birth in generation |
|
156 | - * - The average year of birth in generation |
|
157 | - * |
|
158 | - * @return array<int, array<string, int|null>> Statistics array |
|
159 | - */ |
|
160 | - public function statisticsByGenerations(): array |
|
161 | - { |
|
162 | - $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
163 | - $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
164 | - |
|
165 | - $statistics_by_gen = []; |
|
166 | - foreach ($stats_by_gen as $gen => $stats_gen) { |
|
167 | - $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
168 | - 'sosaCount' => (int) $stats_gen->total_sosa, |
|
169 | - 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
170 | - 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
171 | - 'firstBirth' => $stats_gen->first_year, |
|
172 | - 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
173 | - 'lastBirth' => $stats_gen->last_year, |
|
174 | - 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - return $statistics_by_gen; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Returns the basic statistics data by generation. |
|
183 | - * |
|
184 | - * @return Collection<int, \stdClass> |
|
185 | - */ |
|
186 | - private function statisticsByGenerationBasicData(): Collection |
|
187 | - { |
|
188 | - return DB::table('maj_sosa') |
|
189 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
190 | - ->where('majs_user_id', '=', $this->user->id()) |
|
191 | - ->groupBy('majs_gen') |
|
192 | - ->orderBy('majs_gen', 'asc') |
|
193 | - ->select('majs_gen AS gen') |
|
194 | - ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
195 | - ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
196 | - ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
197 | - ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
198 | - ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
199 | - ->get()->keyBy('gen'); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns the cumulative statistics data by generation |
|
204 | - * |
|
205 | - * @return Collection<int, \stdClass> |
|
206 | - */ |
|
207 | - private function statisticsByGenerationCumulativeData(): Collection |
|
208 | - { |
|
209 | - $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
210 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
211 | - ->where('majs_user_id', '=', $this->user->id()); |
|
212 | - |
|
213 | - return DB::table('maj_sosa') |
|
214 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
215 | - $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
216 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
217 | - ->where('majs_user_id', '=', $this->user->id()); |
|
218 | - }) |
|
219 | - ->groupBy('list_gen.majs_gen') |
|
220 | - ->select('list_gen.majs_gen AS gen') |
|
221 | - ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
222 | - ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
223 | - ->selectRaw('1 - COUNT(DISTINCT majs_i_id) / COUNT(majs_i_id) AS pedi_collapse_simple') |
|
224 | - ->get()->keyBy('gen'); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Returns the pedigree collapse improved calculation by generation. |
|
229 | - * |
|
230 | - * Format: |
|
231 | - * - key : generation |
|
232 | - * - values: |
|
233 | - * - pedi_collapse_roots : pedigree collapse of ancestor roots for the generation |
|
234 | - * - pedi_collapse_xgen : pedigree cross-generation shrinkage for the generation |
|
235 | - * |
|
236 | - * @return array<int, array<string, float>> |
|
237 | - */ |
|
238 | - public function pedigreeCollapseByGenerationData(): array |
|
239 | - { |
|
240 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
241 | - |
|
242 | - $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
243 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
244 | - ->where('majs_user_id', '=', $this->user->id()); |
|
245 | - |
|
246 | - /* Compute the contributions of nodes of previous generations to the current generation */ |
|
247 | - $root_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
248 | - ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
249 | - ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
250 | - ->selectRaw( |
|
251 | - '(CASE ' . |
|
252 | - ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' . |
|
253 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
254 | - ' ELSE 0 ' . |
|
255 | - ' END)' . |
|
256 | - ' + (CASE ' . |
|
257 | - ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' . |
|
258 | - ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
259 | - ' ELSE 0 ' . |
|
260 | - ' END) contrib' |
|
261 | - ) |
|
262 | - ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
263 | - $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen') |
|
264 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
265 | - ->where('majs_user_id', '=', $this->user->id()); |
|
266 | - }) |
|
267 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
268 | - // Link to sosa's father |
|
269 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
270 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
271 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
272 | - }) |
|
273 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
274 | - // Link to sosa's mother |
|
275 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
276 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
277 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
278 | - }) |
|
279 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
280 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
281 | - ->where(function (Builder $query): void { |
|
282 | - $query->whereNull('sosa_fat.majs_i_id') |
|
283 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
284 | - }); |
|
285 | - |
|
286 | - /* Identify nodes in the generations with ancestors who are also in the same generation. |
|
32 | + private UserInterface $user; |
|
33 | + private Tree $tree; |
|
34 | + |
|
35 | + /** |
|
36 | + * Constructor for Sosa Statistics Service |
|
37 | + * |
|
38 | + * @param Tree $tree |
|
39 | + * @param UserInterface $user |
|
40 | + */ |
|
41 | + public function __construct(Tree $tree, UserInterface $user) |
|
42 | + { |
|
43 | + $this->tree = $tree; |
|
44 | + $this->user = $user; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Return the root individual for the reference tree and user. |
|
49 | + * |
|
50 | + * @return Individual|NULL |
|
51 | + */ |
|
52 | + public function rootIndividual(): ?Individual |
|
53 | + { |
|
54 | + $root_indi_id = $this->tree->getUserPreference($this->user, 'MAJ_SOSA_ROOT_ID'); |
|
55 | + return Registry::individualFactory()->make($root_indi_id, $this->tree); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Get the highest generation for the reference tree and user. |
|
60 | + * |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function maxGeneration(): int |
|
64 | + { |
|
65 | + return (int) DB::table('maj_sosa') |
|
66 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
67 | + ->where('majs_user_id', '=', $this->user->id()) |
|
68 | + ->max('majs_gen'); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get the total count of individuals in the tree. |
|
73 | + * |
|
74 | + * @return int |
|
75 | + */ |
|
76 | + public function totalIndividuals(): int |
|
77 | + { |
|
78 | + return DB::table('individuals') |
|
79 | + ->where('i_file', '=', $this->tree->id()) |
|
80 | + ->count(); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get the total count of Sosa ancestors for all generations |
|
85 | + * |
|
86 | + * @return int |
|
87 | + */ |
|
88 | + public function totalAncestors(): int |
|
89 | + { |
|
90 | + return DB::table('maj_sosa') |
|
91 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
92 | + ->where('majs_user_id', '=', $this->user->id()) |
|
93 | + ->count(); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get the total count of Sosa ancestors for a generation |
|
98 | + * |
|
99 | + * @return int |
|
100 | + */ |
|
101 | + public function totalAncestorsAtGeneration(int $gen): int |
|
102 | + { |
|
103 | + return DB::table('maj_sosa') |
|
104 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
105 | + ->where('majs_user_id', '=', $this->user->id()) |
|
106 | + ->where('majs_gen', '=', $gen) |
|
107 | + ->count(); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get the total count of distinct Sosa ancestors for all generations |
|
112 | + * |
|
113 | + * @return int |
|
114 | + */ |
|
115 | + public function totalDistinctAncestors(): int |
|
116 | + { |
|
117 | + return DB::table('maj_sosa') |
|
118 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
119 | + ->where('majs_user_id', '=', $this->user->id()) |
|
120 | + ->distinct() |
|
121 | + ->count('majs_i_id'); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Get the mean generation time, as the slope of the linear regression of birth years vs generations |
|
126 | + * |
|
127 | + * @return float |
|
128 | + */ |
|
129 | + public function meanGenerationTime(): float |
|
130 | + { |
|
131 | + $row = DB::table('maj_sosa') |
|
132 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
133 | + ->where('majs_user_id', '=', $this->user->id()) |
|
134 | + ->whereNotNull('majs_birth_year') |
|
135 | + ->selectRaw('COUNT(majs_sosa) AS n') |
|
136 | + ->selectRaw('SUM(majs_gen * majs_birth_year) AS sum_xy') |
|
137 | + ->selectRaw('SUM(majs_gen) AS sum_x') |
|
138 | + ->selectRaw('SUM(majs_birth_year) AS sum_y') |
|
139 | + ->selectRaw('SUM(majs_gen * majs_gen) AS sum_x2') |
|
140 | + ->get()->first(); |
|
141 | + |
|
142 | + return $row->n == 0 ? 0 : |
|
143 | + -($row->n * $row->sum_xy - $row->sum_x * $row->sum_y) / ($row->n * $row->sum_x2 - pow($row->sum_x, 2)); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get the statistic array detailed by generation. |
|
148 | + * Statistics for each generation are: |
|
149 | + * - The number of Sosa in generation |
|
150 | + * - The number of Sosa up to generation |
|
151 | + * - The number of distinct Sosa up to generation |
|
152 | + * - The year of the first birth in generation |
|
153 | + * - The year of the first estimated birth in generation |
|
154 | + * - The year of the last birth in generation |
|
155 | + * - The year of the last estimated birth in generation |
|
156 | + * - The average year of birth in generation |
|
157 | + * |
|
158 | + * @return array<int, array<string, int|null>> Statistics array |
|
159 | + */ |
|
160 | + public function statisticsByGenerations(): array |
|
161 | + { |
|
162 | + $stats_by_gen = $this->statisticsByGenerationBasicData(); |
|
163 | + $cumul_stats_by_gen = $this->statisticsByGenerationCumulativeData(); |
|
164 | + |
|
165 | + $statistics_by_gen = []; |
|
166 | + foreach ($stats_by_gen as $gen => $stats_gen) { |
|
167 | + $statistics_by_gen[(int) $stats_gen->gen] = array( |
|
168 | + 'sosaCount' => (int) $stats_gen->total_sosa, |
|
169 | + 'sosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_cumul, |
|
170 | + 'diffSosaTotalCount' => (int) $cumul_stats_by_gen[$gen]->total_distinct_cumul, |
|
171 | + 'firstBirth' => $stats_gen->first_year, |
|
172 | + 'firstEstimatedBirth' => $stats_gen->first_est_year, |
|
173 | + 'lastBirth' => $stats_gen->last_year, |
|
174 | + 'lastEstimatedBirth' => $stats_gen->last_est_year |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + return $statistics_by_gen; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Returns the basic statistics data by generation. |
|
183 | + * |
|
184 | + * @return Collection<int, \stdClass> |
|
185 | + */ |
|
186 | + private function statisticsByGenerationBasicData(): Collection |
|
187 | + { |
|
188 | + return DB::table('maj_sosa') |
|
189 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
190 | + ->where('majs_user_id', '=', $this->user->id()) |
|
191 | + ->groupBy('majs_gen') |
|
192 | + ->orderBy('majs_gen', 'asc') |
|
193 | + ->select('majs_gen AS gen') |
|
194 | + ->selectRaw('COUNT(majs_sosa) AS total_sosa') |
|
195 | + ->selectRaw('MIN(majs_birth_year) AS first_year') |
|
196 | + ->selectRaw('MIN(majs_birth_year_est) AS first_est_year') |
|
197 | + ->selectRaw('MAX(majs_birth_year) AS last_year') |
|
198 | + ->selectRaw('MAX(majs_birth_year_est) AS last_est_year') |
|
199 | + ->get()->keyBy('gen'); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns the cumulative statistics data by generation |
|
204 | + * |
|
205 | + * @return Collection<int, \stdClass> |
|
206 | + */ |
|
207 | + private function statisticsByGenerationCumulativeData(): Collection |
|
208 | + { |
|
209 | + $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
210 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
211 | + ->where('majs_user_id', '=', $this->user->id()); |
|
212 | + |
|
213 | + return DB::table('maj_sosa') |
|
214 | + ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
215 | + $join->on('maj_sosa.majs_gen', '<=', 'list_gen.majs_gen') |
|
216 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
217 | + ->where('majs_user_id', '=', $this->user->id()); |
|
218 | + }) |
|
219 | + ->groupBy('list_gen.majs_gen') |
|
220 | + ->select('list_gen.majs_gen AS gen') |
|
221 | + ->selectRaw('COUNT(majs_i_id) AS total_cumul') |
|
222 | + ->selectRaw('COUNT(DISTINCT majs_i_id) AS total_distinct_cumul') |
|
223 | + ->selectRaw('1 - COUNT(DISTINCT majs_i_id) / COUNT(majs_i_id) AS pedi_collapse_simple') |
|
224 | + ->get()->keyBy('gen'); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Returns the pedigree collapse improved calculation by generation. |
|
229 | + * |
|
230 | + * Format: |
|
231 | + * - key : generation |
|
232 | + * - values: |
|
233 | + * - pedi_collapse_roots : pedigree collapse of ancestor roots for the generation |
|
234 | + * - pedi_collapse_xgen : pedigree cross-generation shrinkage for the generation |
|
235 | + * |
|
236 | + * @return array<int, array<string, float>> |
|
237 | + */ |
|
238 | + public function pedigreeCollapseByGenerationData(): array |
|
239 | + { |
|
240 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
241 | + |
|
242 | + $list_gen = DB::table('maj_sosa')->select('majs_gen')->distinct() |
|
243 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
244 | + ->where('majs_user_id', '=', $this->user->id()); |
|
245 | + |
|
246 | + /* Compute the contributions of nodes of previous generations to the current generation */ |
|
247 | + $root_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
248 | + ->select(['list_gen.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
249 | + ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
250 | + ->selectRaw( |
|
251 | + '(CASE ' . |
|
252 | + ' WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL' . |
|
253 | + ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
254 | + ' ELSE 0 ' . |
|
255 | + ' END)' . |
|
256 | + ' + (CASE ' . |
|
257 | + ' WHEN ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL' . |
|
258 | + ' THEN POWER(2, ' . $table_prefix . 'list_gen.majs_gen - ' . $table_prefix . 'sosa.majs_gen - 1)' . |
|
259 | + ' ELSE 0 ' . |
|
260 | + ' END) contrib' |
|
261 | + ) |
|
262 | + ->joinSub($list_gen, 'list_gen', function (JoinClause $join): void { |
|
263 | + $join->on('sosa.majs_gen', '<', 'list_gen.majs_gen') |
|
264 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
265 | + ->where('majs_user_id', '=', $this->user->id()); |
|
266 | + }) |
|
267 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
268 | + // Link to sosa's father |
|
269 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
270 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
271 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
272 | + }) |
|
273 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
274 | + // Link to sosa's mother |
|
275 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
276 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
277 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
278 | + }) |
|
279 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
280 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
281 | + ->where(function (Builder $query): void { |
|
282 | + $query->whereNull('sosa_fat.majs_i_id') |
|
283 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
284 | + }); |
|
285 | + |
|
286 | + /* Identify nodes in the generations with ancestors who are also in the same generation. |
|
287 | 287 | * This is the vertical/generational collapse that will reduce the number or roots. |
288 | 288 | */ |
289 | - $non_roots_ancestors = DB::table('maj_sosa AS sosa') |
|
290 | - ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa']) |
|
291 | - ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' . |
|
292 | - ' AS full_ancestors') |
|
293 | - ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void { |
|
294 | - $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen') |
|
295 | - ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' . |
|
296 | - $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' . |
|
297 | - $table_prefix . 'sosa.majs_sosa') |
|
298 | - ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id()) |
|
299 | - ->where('sosa_anc.majs_user_id', '=', $this->user->id()); |
|
300 | - }) |
|
301 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
302 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
303 | - ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void { |
|
304 | - $query->from('maj_sosa AS sosa_gen') |
|
305 | - ->select('sosa_gen.majs_i_id')->distinct() |
|
306 | - ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id()) |
|
307 | - ->where('sosa_gen.majs_user_id', '=', $this->user->id()) |
|
308 | - ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen'); |
|
309 | - }) |
|
310 | - ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', |
|
311 | - 'sosa.majs_sosa', 'sosa.majs_i_id']); |
|
312 | - |
|
313 | - /* Compute the contribution of the nodes in the generation, |
|
289 | + $non_roots_ancestors = DB::table('maj_sosa AS sosa') |
|
290 | + ->select(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', 'sosa.majs_sosa']) |
|
291 | + ->selectRaw('MAX(' . $table_prefix . 'sosa_anc.majs_sosa) - MIN(' . $table_prefix . 'sosa_anc.majs_sosa)' . |
|
292 | + ' AS full_ancestors') |
|
293 | + ->join('maj_sosa AS sosa_anc', function (JoinClause $join) use ($table_prefix): void { |
|
294 | + $join->on('sosa.majs_gen', '<', 'sosa_anc.majs_gen') |
|
295 | + ->whereRaw('FLOOR(' . $table_prefix . 'sosa_anc.majs_sosa / POWER(2, ' . |
|
296 | + $table_prefix . 'sosa_anc.majs_gen - ' . $table_prefix . 'sosa.majs_gen)) = ' . |
|
297 | + $table_prefix . 'sosa.majs_sosa') |
|
298 | + ->where('sosa_anc.majs_gedcom_id', '=', $this->tree->id()) |
|
299 | + ->where('sosa_anc.majs_user_id', '=', $this->user->id()); |
|
300 | + }) |
|
301 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
302 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
303 | + ->whereIn('sosa_anc.majs_i_id', function (Builder $query) use ($table_prefix): void { |
|
304 | + $query->from('maj_sosa AS sosa_gen') |
|
305 | + ->select('sosa_gen.majs_i_id')->distinct() |
|
306 | + ->where('sosa_gen.majs_gedcom_id', '=', $this->tree->id()) |
|
307 | + ->where('sosa_gen.majs_user_id', '=', $this->user->id()) |
|
308 | + ->whereRaw($table_prefix . 'sosa_gen.majs_gen = ' . $table_prefix . 'sosa.majs_gen'); |
|
309 | + }) |
|
310 | + ->groupBy(['sosa.majs_gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id', |
|
311 | + 'sosa.majs_sosa', 'sosa.majs_i_id']); |
|
312 | + |
|
313 | + /* Compute the contribution of the nodes in the generation, |
|
314 | 314 | * excluding the nodes with ancestors in the same generation. |
315 | 315 | * Nodes with a parent missing are not excluded to cater for the missing one. |
316 | 316 | */ |
317 | - $known_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
318 | - ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
319 | - ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
320 | - ->selectRaw('1 AS contrib') |
|
321 | - ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void { |
|
322 | - $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen') |
|
323 | - ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa') |
|
324 | - ->where('nonroot.full_ancestors', '>', 0) |
|
325 | - ->where('nonroot.majs_gedcom_id', '=', $this->tree->id()) |
|
326 | - ->where('nonroot.majs_user_id', '=', $this->user->id()); |
|
327 | - }) |
|
328 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
329 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
330 | - ->whereNull('nonroot.majs_sosa'); |
|
331 | - |
|
332 | - /* Aggregate both queries, and calculate the sum of contributions by generation roots. |
|
317 | + $known_ancestors_contributions = DB::table('maj_sosa AS sosa') |
|
318 | + ->select(['sosa.majs_gen AS gen', 'sosa.majs_gedcom_id', 'sosa.majs_user_id']) |
|
319 | + ->addSelect(['sosa.majs_i_id', 'sosa.majs_gen']) |
|
320 | + ->selectRaw('1 AS contrib') |
|
321 | + ->leftJoinSub($non_roots_ancestors, 'nonroot', function (JoinClause $join): void { |
|
322 | + $join->on('sosa.majs_gen', '=', 'nonroot.majs_gen') |
|
323 | + ->on('sosa.majs_sosa', '=', 'nonroot.majs_sosa') |
|
324 | + ->where('nonroot.full_ancestors', '>', 0) |
|
325 | + ->where('nonroot.majs_gedcom_id', '=', $this->tree->id()) |
|
326 | + ->where('nonroot.majs_user_id', '=', $this->user->id()); |
|
327 | + }) |
|
328 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
329 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
330 | + ->whereNull('nonroot.majs_sosa'); |
|
331 | + |
|
332 | + /* Aggregate both queries, and calculate the sum of contributions by generation roots. |
|
333 | 333 | * Exclude as well nodes that already appear in lower generations, as their branche has already been reduced. |
334 | 334 | */ |
335 | - $ancestors_contributions_sum = DB::connection()->query() |
|
336 | - ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs') |
|
337 | - ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id']) |
|
338 | - ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib']) |
|
339 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' . |
|
340 | - $table_prefix . 'sosa_contribs.contrib AS totalContrib') |
|
341 | - ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void { |
|
342 | - $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen') |
|
343 | - ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id') |
|
344 | - ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id()) |
|
345 | - ->where('sosa_low.majs_user_id', '=', $this->user->id()); |
|
346 | - }) |
|
347 | - ->whereNull('sosa_low.majs_sosa') |
|
348 | - ->groupBy(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id', |
|
349 | - 'sosa_contribs.majs_i_id', 'sosa_contribs.contrib']); |
|
350 | - |
|
351 | - // Aggregate all generation roots to compute root and generation pedigree collapse |
|
352 | - $pedi_collapse_coll = DB::connection()->query()->fromSub($ancestors_contributions_sum, 'sosa_contribs_sum') |
|
353 | - ->select(['gen'])->selectRaw('SUM(contrib), SUM(totalContrib)') |
|
354 | - ->selectRaw('1 - SUM(contrib) / SUM(totalContrib) AS pedi_collapse_roots') // Roots/horizontal collapse |
|
355 | - ->selectRaw('1 - SUM(totalContrib) / POWER ( 2, gen - 1) AS pedi_collapse_xgen') // Crossgeneration collapse |
|
356 | - ->groupBy(['gen', 'majs_gedcom_id', 'majs_user_id']) |
|
357 | - ->orderBy('gen') |
|
358 | - ->get(); |
|
359 | - |
|
360 | - $pedi_collapse_by_gen = []; |
|
361 | - foreach ($pedi_collapse_coll as $collapse_gen) { |
|
362 | - $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array( |
|
363 | - 'pedi_collapse_roots' => (float) $collapse_gen->pedi_collapse_roots, |
|
364 | - 'pedi_collapse_xgen' => (float) $collapse_gen->pedi_collapse_xgen |
|
365 | - ); |
|
366 | - } |
|
367 | - return $pedi_collapse_by_gen; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
372 | - * Sosa 1 is of generation 1. |
|
373 | - * |
|
374 | - * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
375 | - * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
376 | - * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
377 | - * |
|
378 | - * Format: |
|
379 | - * - key : sosa number of the ancestor |
|
380 | - * - values: |
|
381 | - * - root_ancestor_id : ID of the ancestor |
|
382 | - * - mean_gen_depth : Mean generation depth |
|
383 | - * - stddev_gen_depth : Standard deviation of generation depth |
|
384 | - * |
|
385 | - * @param int $gen Sosa generation |
|
386 | - * @return Collection<int, \stdClass> |
|
387 | - */ |
|
388 | - public function generationDepthStatsAtGeneration(int $gen): Collection |
|
389 | - { |
|
390 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
391 | - $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
392 | - ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
393 | - ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
394 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
395 | - ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
396 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
397 | - // Link to sosa's father |
|
398 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
399 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
400 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
401 | - }) |
|
402 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
403 | - // Link to sosa's mother |
|
404 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
405 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
406 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
407 | - }) |
|
408 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
409 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
410 | - ->where('sosa.majs_gen', '>=', $gen) |
|
411 | - ->where(function (Builder $query): void { |
|
412 | - $query->whereNull('sosa_fat.majs_i_id') |
|
413 | - ->orWhereNull('sosa_mot.majs_i_id'); |
|
414 | - }) |
|
415 | - ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
416 | - |
|
417 | - return DB::table('maj_sosa AS sosa_list') |
|
418 | - ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
419 | - ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
420 | - ->selectRaw(' SQRT(' . |
|
421 | - ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
422 | - ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
423 | - ' ) AS stddev_gen_depth') |
|
424 | - ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
425 | - $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
426 | - ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
427 | - ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
428 | - }) |
|
429 | - ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
430 | - ->orderBy('stats_by_gen.root_ancestor') |
|
431 | - ->get()->keyBy('root_ancestor_sosa'); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Return a collection of the most duplicated root Sosa ancestors. |
|
436 | - * The number of ancestors to return is limited by the parameter $limit. |
|
437 | - * If several individuals are tied when reaching the limit, none of them are returned, |
|
438 | - * which means that there can be less individuals returned than requested. |
|
439 | - * |
|
440 | - * Format: |
|
441 | - * - value: |
|
442 | - * - sosa_i_id : sosa individual |
|
443 | - * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
444 | - * |
|
445 | - * @param int $limit |
|
446 | - * @return Collection<\stdClass> |
|
447 | - */ |
|
448 | - public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
449 | - { |
|
450 | - $table_prefix = DB::connection()->getTablePrefix(); |
|
451 | - $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
452 | - ->select('sosa.majs_i_id AS sosa_i_id') |
|
453 | - ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
454 | - ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
455 | - // Link to sosa's father |
|
456 | - $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
457 | - ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
458 | - ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
459 | - }) |
|
460 | - ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
461 | - // Link to sosa's mother |
|
462 | - $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
463 | - ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
464 | - ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
465 | - }) |
|
466 | - ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
467 | - ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
468 | - ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
469 | - ->whereNull('sosa_mot.majs_sosa') |
|
470 | - ->groupBy('sosa.majs_i_id') |
|
471 | - ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
472 | - ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
473 | - ->limit($limit + 1) // We want to select one more than required, for ties |
|
474 | - ->get(); |
|
475 | - |
|
476 | - if ($multiple_ancestors->count() > $limit) { |
|
477 | - $last_count = $multiple_ancestors->last()->sosa_count; |
|
478 | - $multiple_ancestors = $multiple_ancestors->reject( |
|
479 | - fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
480 | - ); |
|
481 | - } |
|
482 | - return $multiple_ancestors; |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
487 | - * at a specified generation. |
|
488 | - * |
|
489 | - * Format: |
|
490 | - * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
491 | - * For instance 3 represent the maternal grand father |
|
492 | - * 0 is used for shared ancestors |
|
493 | - * - values: |
|
494 | - * - branches: same as key |
|
495 | - * - majs_i_id: xref of the ancestor at rank key in generation G, or null for shared ancestors |
|
496 | - * - count_indi: number of ancestors exclusively in the ancestors of the ancestor at rank key |
|
497 | - * |
|
498 | - * For instance a result at generation 3 could be : |
|
499 | - * [ |
|
500 | - * 0 => { branches: 0, majs_i_id: X1, count_indi: 12 } -> 12 ancestors are shared by the grand-parents |
|
501 | - * 1 => { branches: 1, majs_i_id: X2, count_indi: 32 } -> 32 ancestors are exclusive to the paternal grand-father |
|
502 | - * 2 => { branches: 2, majs_i_id: X3, count_indi: 25 } -> 25 ancestors are exclusive to the paternal grand-mother |
|
503 | - * 3 => { branches: 3, majs_i_id: X4, count_indi: 12 } -> 12 ancestors are exclusive to the maternal grand-father |
|
504 | - * 4 => { branches: 4, majs_i_id: X5, count_indi: 30 } -> 30 ancestors are exclusive to the maternal grand-mother |
|
505 | - * ] |
|
506 | - * |
|
507 | - * @param int $gen |
|
508 | - * @return Collection<int, \stdClass> |
|
509 | - */ |
|
510 | - public function ancestorsDispersionForGeneration(int $gen): Collection |
|
511 | - { |
|
512 | - $ancestors_branches = DB::table('maj_sosa') |
|
513 | - ->select('majs_i_id AS i_id') |
|
514 | - ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
515 | - ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
516 | - ->where('majs_user_id', '=', $this->user->id()) |
|
517 | - ->where('majs_gen', '>=', $gen) |
|
518 | - ->groupBy('majs_i_id', 'branch'); |
|
519 | - |
|
520 | - $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
521 | - ->fromSub($ancestors_branches, 'indi_branch') |
|
522 | - ->select('i_id') |
|
523 | - ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
524 | - ->groupBy('i_id'); |
|
525 | - |
|
526 | - return DB::table('maj_sosa') |
|
527 | - ->rightJoinSub( |
|
528 | - $consolidated_ancestors_branches, |
|
529 | - 'indi_branch_consolidated', |
|
530 | - function (JoinClause $join) use ($gen): void { |
|
531 | - $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
532 | - ->where('maj_sosa.majs_user_id', '=', $this->user->id()) |
|
533 | - ->where('branches', '>', 0) |
|
534 | - ->whereRaw('majs_sosa = POW(2, ? - 1) + branches - 1', [$gen]); |
|
535 | - } |
|
536 | - ) |
|
537 | - ->select(['branches', 'majs_i_id']) |
|
538 | - ->selectRaw('COUNT(i_id) AS count_indi') |
|
539 | - ->groupBy(['branches', 'majs_i_id']) |
|
540 | - ->get()->keyBy('branches'); |
|
541 | - } |
|
335 | + $ancestors_contributions_sum = DB::connection()->query() |
|
336 | + ->fromSub($root_ancestors_contributions->unionAll($known_ancestors_contributions), 'sosa_contribs') |
|
337 | + ->select(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id']) |
|
338 | + ->addSelect(['sosa_contribs.majs_i_id', 'sosa_contribs.contrib']) |
|
339 | + ->selectRaw('COUNT(' . $table_prefix . 'sosa_contribs.majs_i_id) * ' . |
|
340 | + $table_prefix . 'sosa_contribs.contrib AS totalContrib') |
|
341 | + ->leftJoin('maj_sosa AS sosa_low', function (JoinClause $join): void { |
|
342 | + $join->on('sosa_low.majs_gen', '<', 'sosa_contribs.majs_gen') |
|
343 | + ->on('sosa_low.majs_i_id', '=', 'sosa_contribs.majs_i_id') |
|
344 | + ->where('sosa_low.majs_gedcom_id', '=', $this->tree->id()) |
|
345 | + ->where('sosa_low.majs_user_id', '=', $this->user->id()); |
|
346 | + }) |
|
347 | + ->whereNull('sosa_low.majs_sosa') |
|
348 | + ->groupBy(['sosa_contribs.gen', 'sosa_contribs.majs_gedcom_id', 'sosa_contribs.majs_user_id', |
|
349 | + 'sosa_contribs.majs_i_id', 'sosa_contribs.contrib']); |
|
350 | + |
|
351 | + // Aggregate all generation roots to compute root and generation pedigree collapse |
|
352 | + $pedi_collapse_coll = DB::connection()->query()->fromSub($ancestors_contributions_sum, 'sosa_contribs_sum') |
|
353 | + ->select(['gen'])->selectRaw('SUM(contrib), SUM(totalContrib)') |
|
354 | + ->selectRaw('1 - SUM(contrib) / SUM(totalContrib) AS pedi_collapse_roots') // Roots/horizontal collapse |
|
355 | + ->selectRaw('1 - SUM(totalContrib) / POWER ( 2, gen - 1) AS pedi_collapse_xgen') // Crossgeneration collapse |
|
356 | + ->groupBy(['gen', 'majs_gedcom_id', 'majs_user_id']) |
|
357 | + ->orderBy('gen') |
|
358 | + ->get(); |
|
359 | + |
|
360 | + $pedi_collapse_by_gen = []; |
|
361 | + foreach ($pedi_collapse_coll as $collapse_gen) { |
|
362 | + $pedi_collapse_by_gen[(int) $collapse_gen->gen] = array( |
|
363 | + 'pedi_collapse_roots' => (float) $collapse_gen->pedi_collapse_roots, |
|
364 | + 'pedi_collapse_xgen' => (float) $collapse_gen->pedi_collapse_xgen |
|
365 | + ); |
|
366 | + } |
|
367 | + return $pedi_collapse_by_gen; |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Return a Collection of the mean generation depth and deviation for all Sosa ancestors at a given generation. |
|
372 | + * Sosa 1 is of generation 1. |
|
373 | + * |
|
374 | + * Mean generation depth and deviation are calculated based on the works of Marie-Héléne Cazes and Pierre Cazes, |
|
375 | + * published in Population (French Edition), Vol. 51, No. 1 (Jan. - Feb., 1996), pp. 117-140 |
|
376 | + * http://kintip.net/index.php?option=com_jdownloads&task=download.send&id=9&catid=4&m=0 |
|
377 | + * |
|
378 | + * Format: |
|
379 | + * - key : sosa number of the ancestor |
|
380 | + * - values: |
|
381 | + * - root_ancestor_id : ID of the ancestor |
|
382 | + * - mean_gen_depth : Mean generation depth |
|
383 | + * - stddev_gen_depth : Standard deviation of generation depth |
|
384 | + * |
|
385 | + * @param int $gen Sosa generation |
|
386 | + * @return Collection<int, \stdClass> |
|
387 | + */ |
|
388 | + public function generationDepthStatsAtGeneration(int $gen): Collection |
|
389 | + { |
|
390 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
391 | + $missing_ancestors_by_gen = DB::table('maj_sosa AS sosa') |
|
392 | + ->selectRaw($table_prefix . 'sosa.majs_gen - ? AS majs_gen_norm', [$gen]) |
|
393 | + ->selectRaw('FLOOR(((' . $table_prefix . 'sosa.majs_sosa / POW(2, ' . $table_prefix . 'sosa.majs_gen -1 )) - 1) * POWER(2, ? - 1)) + POWER(2, ? - 1) AS root_ancestor', [$gen, $gen]) //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
394 | + ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 1 ELSE 0 END) AS full_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
395 | + ->selectRaw('SUM(CASE WHEN ' . $table_prefix . 'sosa_fat.majs_i_id IS NULL AND ' . $table_prefix . 'sosa_mot.majs_i_id IS NULL THEN 0 ELSE 1 END) As semi_root_count') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
396 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
397 | + // Link to sosa's father |
|
398 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
399 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
400 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
401 | + }) |
|
402 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
403 | + // Link to sosa's mother |
|
404 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
405 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
406 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
407 | + }) |
|
408 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
409 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
410 | + ->where('sosa.majs_gen', '>=', $gen) |
|
411 | + ->where(function (Builder $query): void { |
|
412 | + $query->whereNull('sosa_fat.majs_i_id') |
|
413 | + ->orWhereNull('sosa_mot.majs_i_id'); |
|
414 | + }) |
|
415 | + ->groupBy(['sosa.majs_gen', 'root_ancestor']); |
|
416 | + |
|
417 | + return DB::table('maj_sosa AS sosa_list') |
|
418 | + ->select(['stats_by_gen.root_ancestor AS root_ancestor_sosa', 'sosa_list.majs_i_id as root_ancestor_id']) |
|
419 | + ->selectRaw('1 + SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))) AS mean_gen_depth') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
420 | + ->selectRaw(' SQRT(' . |
|
421 | + ' SUM(POWER(majs_gen_norm, 2) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm)))' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
422 | + ' - POWER( SUM( (majs_gen_norm) * ( 2 * full_root_count + semi_root_count) / (2 * POWER(2, majs_gen_norm))), 2)' . //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
423 | + ' ) AS stddev_gen_depth') |
|
424 | + ->joinSub($missing_ancestors_by_gen, 'stats_by_gen', function (JoinClause $join): void { |
|
425 | + $join->on('sosa_list.majs_sosa', '=', 'stats_by_gen.root_ancestor') |
|
426 | + ->where('sosa_list.majs_gedcom_id', '=', $this->tree->id()) |
|
427 | + ->where('sosa_list.majs_user_id', '=', $this->user->id()); |
|
428 | + }) |
|
429 | + ->groupBy(['stats_by_gen.root_ancestor', 'sosa_list.majs_i_id']) |
|
430 | + ->orderBy('stats_by_gen.root_ancestor') |
|
431 | + ->get()->keyBy('root_ancestor_sosa'); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Return a collection of the most duplicated root Sosa ancestors. |
|
436 | + * The number of ancestors to return is limited by the parameter $limit. |
|
437 | + * If several individuals are tied when reaching the limit, none of them are returned, |
|
438 | + * which means that there can be less individuals returned than requested. |
|
439 | + * |
|
440 | + * Format: |
|
441 | + * - value: |
|
442 | + * - sosa_i_id : sosa individual |
|
443 | + * - sosa_count: number of duplications of the ancestor (e.g. 3 if it appears 3 times) |
|
444 | + * |
|
445 | + * @param int $limit |
|
446 | + * @return Collection<\stdClass> |
|
447 | + */ |
|
448 | + public function topMultipleAncestorsWithNoTies(int $limit): Collection |
|
449 | + { |
|
450 | + $table_prefix = DB::connection()->getTablePrefix(); |
|
451 | + $multiple_ancestors = DB::table('maj_sosa AS sosa') |
|
452 | + ->select('sosa.majs_i_id AS sosa_i_id') |
|
453 | + ->selectRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) AS sosa_count') |
|
454 | + ->leftJoin('maj_sosa AS sosa_fat', function (JoinClause $join) use ($table_prefix): void { |
|
455 | + // Link to sosa's father |
|
456 | + $join->whereRaw($table_prefix . 'sosa_fat.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa') |
|
457 | + ->where('sosa_fat.majs_gedcom_id', '=', $this->tree->id()) |
|
458 | + ->where('sosa_fat.majs_user_id', '=', $this->user->id()); |
|
459 | + }) |
|
460 | + ->leftJoin('maj_sosa AS sosa_mot', function (JoinClause $join) use ($table_prefix): void { |
|
461 | + // Link to sosa's mother |
|
462 | + $join->whereRaw($table_prefix . 'sosa_mot.majs_sosa = 2 * ' . $table_prefix . 'sosa.majs_sosa + 1') |
|
463 | + ->where('sosa_mot.majs_gedcom_id', '=', $this->tree->id()) |
|
464 | + ->where('sosa_mot.majs_user_id', '=', $this->user->id()); |
|
465 | + }) |
|
466 | + ->where('sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
467 | + ->where('sosa.majs_user_id', '=', $this->user->id()) |
|
468 | + ->whereNull('sosa_fat.majs_sosa') // We keep only root individuals, i.e. those with no father or mother |
|
469 | + ->whereNull('sosa_mot.majs_sosa') |
|
470 | + ->groupBy('sosa.majs_i_id') |
|
471 | + ->havingRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) > 1') // Limit to the duplicate sosas. |
|
472 | + ->orderByRaw('COUNT(' . $table_prefix . 'sosa.majs_sosa) DESC, MIN(' . $table_prefix . 'sosa.majs_sosa) ASC') //@phpcs:ignore Generic.Files.LineLength.TooLong |
|
473 | + ->limit($limit + 1) // We want to select one more than required, for ties |
|
474 | + ->get(); |
|
475 | + |
|
476 | + if ($multiple_ancestors->count() > $limit) { |
|
477 | + $last_count = $multiple_ancestors->last()->sosa_count; |
|
478 | + $multiple_ancestors = $multiple_ancestors->reject( |
|
479 | + fn (stdClass $element): bool => $element->sosa_count === $last_count |
|
480 | + ); |
|
481 | + } |
|
482 | + return $multiple_ancestors; |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Return a computed array of statistics about the dispersion of ancestors across the ancestors |
|
487 | + * at a specified generation. |
|
488 | + * |
|
489 | + * Format: |
|
490 | + * - key : rank of the ancestor in generation G for which exclusive ancestors have been found |
|
491 | + * For instance 3 represent the maternal grand father |
|
492 | + * 0 is used for shared ancestors |
|
493 | + * - values: |
|
494 | + * - branches: same as key |
|
495 | + * - majs_i_id: xref of the ancestor at rank key in generation G, or null for shared ancestors |
|
496 | + * - count_indi: number of ancestors exclusively in the ancestors of the ancestor at rank key |
|
497 | + * |
|
498 | + * For instance a result at generation 3 could be : |
|
499 | + * [ |
|
500 | + * 0 => { branches: 0, majs_i_id: X1, count_indi: 12 } -> 12 ancestors are shared by the grand-parents |
|
501 | + * 1 => { branches: 1, majs_i_id: X2, count_indi: 32 } -> 32 ancestors are exclusive to the paternal grand-father |
|
502 | + * 2 => { branches: 2, majs_i_id: X3, count_indi: 25 } -> 25 ancestors are exclusive to the paternal grand-mother |
|
503 | + * 3 => { branches: 3, majs_i_id: X4, count_indi: 12 } -> 12 ancestors are exclusive to the maternal grand-father |
|
504 | + * 4 => { branches: 4, majs_i_id: X5, count_indi: 30 } -> 30 ancestors are exclusive to the maternal grand-mother |
|
505 | + * ] |
|
506 | + * |
|
507 | + * @param int $gen |
|
508 | + * @return Collection<int, \stdClass> |
|
509 | + */ |
|
510 | + public function ancestorsDispersionForGeneration(int $gen): Collection |
|
511 | + { |
|
512 | + $ancestors_branches = DB::table('maj_sosa') |
|
513 | + ->select('majs_i_id AS i_id') |
|
514 | + ->selectRaw('FLOOR(majs_sosa / POW(2, (majs_gen - ?))) - POW(2, ? -1) + 1 AS branch', [$gen, $gen]) |
|
515 | + ->where('majs_gedcom_id', '=', $this->tree->id()) |
|
516 | + ->where('majs_user_id', '=', $this->user->id()) |
|
517 | + ->where('majs_gen', '>=', $gen) |
|
518 | + ->groupBy('majs_i_id', 'branch'); |
|
519 | + |
|
520 | + $consolidated_ancestors_branches = DB::table('maj_sosa') |
|
521 | + ->fromSub($ancestors_branches, 'indi_branch') |
|
522 | + ->select('i_id') |
|
523 | + ->selectRaw('CASE WHEN COUNT(branch) > 1 THEN 0 ELSE MIN(branch) END AS branches') |
|
524 | + ->groupBy('i_id'); |
|
525 | + |
|
526 | + return DB::table('maj_sosa') |
|
527 | + ->rightJoinSub( |
|
528 | + $consolidated_ancestors_branches, |
|
529 | + 'indi_branch_consolidated', |
|
530 | + function (JoinClause $join) use ($gen): void { |
|
531 | + $join->where('maj_sosa.majs_gedcom_id', '=', $this->tree->id()) |
|
532 | + ->where('maj_sosa.majs_user_id', '=', $this->user->id()) |
|
533 | + ->where('branches', '>', 0) |
|
534 | + ->whereRaw('majs_sosa = POW(2, ? - 1) + branches - 1', [$gen]); |
|
535 | + } |
|
536 | + ) |
|
537 | + ->select(['branches', 'majs_i_id']) |
|
538 | + ->selectRaw('COUNT(i_id) AS count_indi') |
|
539 | + ->groupBy(['branches', 'majs_i_id']) |
|
540 | + ->get()->keyBy('branches'); |
|
541 | + } |
|
542 | 542 | } |