src/Domain/Zones/Model/TownLocation.php line 25

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Zones\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Location\Coordinate;
  8. use Location\Ellipsoid;
  9. use Location\Formatter\Polygon\GeoJSON;
  10. use Location\Polygon as LocationPolygon;
  11. use Ramsey\Uuid\Uuid;
  12. use Whater\Domain\Common\Exception\InvalidUUIDException;
  13. use LongitudeOne\Spatial\PHP\Types\Geometry\Polygon;
  14. use Whater\Domain\Whater\Model\DistributionNetwork;
  15. use Whater\Domain\Whater\Model\WhaterOrganization;
  16. use Whater\Domain\Zones\Event\TownLocationPolygonWasUpdate;
  17. /**
  18. * Class TownLocation - Localizaciones - (localidades en sinac)
  19. *
  20. * @package Whater\Domain\Zones\Model
  21. */
  22. class TownLocation implements ContainsRecordedEvents
  23. {
  24. use EventRecorderCapabilities;
  25. // const IDS_SINAC_LOCALIDAD = 'sinac_codLocalidad';
  26. const TLT_URBAN_ZONE = 'TLT_URBAN_ZONE';
  27. const TLT_INDUSTRIAL_ZONE = 'TLT_INDUSTRIAL_ZONE';
  28. /**
  29. * @var string
  30. */
  31. private $uuid;
  32. /**
  33. * @var string
  34. */
  35. private $slug;
  36. /**
  37. * @var string
  38. */
  39. private $name;
  40. /**
  41. * @var string
  42. */
  43. private $townLocationType;
  44. /**
  45. * @var array
  46. */
  47. private $externalIds;
  48. /**
  49. * @var int
  50. */
  51. private $population;
  52. /**
  53. * @var float
  54. */
  55. private $latitude;
  56. /**
  57. * @var float
  58. */
  59. private $longitude;
  60. /**
  61. * @var Polygon
  62. */
  63. private $geometryPolygon;
  64. /**
  65. * @var Country
  66. */
  67. private $country;
  68. /**
  69. * @var CountryArea
  70. */
  71. private $countryArea;
  72. /**
  73. * @var Town
  74. */
  75. private $town;
  76. /**
  77. * @var DistributionNetwork
  78. */
  79. private $distributionNetwork;
  80. /**
  81. * @var Collection
  82. */
  83. private $whaterPoints;
  84. /**
  85. * @var \DateTime
  86. */
  87. private $createdAt;
  88. /**
  89. * @var \DateTime
  90. */
  91. private $updatedAt;
  92. /**
  93. * @var \DateTime
  94. */
  95. private $sinacSynchonizationAt;
  96. /**
  97. * @var Collection
  98. */
  99. private $ubications;
  100. /**
  101. * @var Collection
  102. */
  103. private $establishments;
  104. /**
  105. * @var WhaterOrganization
  106. */
  107. private $whaterOrganization;
  108. /**
  109. * @var Collection
  110. */
  111. private $ownershipRequests;
  112. /**
  113. * @param string $townLocationId
  114. * @param Country $country
  115. * @param CountryArea $countryArea
  116. * @param Town $town
  117. * @param DistributionNetwork $distributionNetwork
  118. * @param string $name
  119. * @param float $latitude
  120. * @param float $longitude
  121. * @param string $slug
  122. * @param array $externalIds
  123. */
  124. public function __construct(
  125. string $townLocationId = null,
  126. Country $country,
  127. CountryArea $countryArea,
  128. Town $town,
  129. DistributionNetwork $distributionNetwork = null,
  130. string $name,
  131. string $townLocationType,
  132. float $latitude,
  133. float $longitude,
  134. string $slug = null,
  135. array $externalIds = []
  136. ) {
  137. try {
  138. $this->uuid = Uuid::fromString($townLocationId ?: Uuid::uuid4())->toString();
  139. } catch (\InvalidArgumentException $e) {
  140. throw new InvalidUUIDException();
  141. }
  142. $this->country = $country;
  143. if ($countryArea != null) {
  144. $this->countryArea = $countryArea;
  145. $this->country = $countryArea->country();
  146. }
  147. $this->town = $town;
  148. $this->distributionNetwork = $distributionNetwork;
  149. $this->slug = $slug;
  150. $this->name = $name;
  151. $this->townLocationType = $townLocationType;
  152. $this->externalIds = $externalIds;
  153. $this->latitude = $latitude;
  154. $this->longitude = $longitude;
  155. $this->geometryPolygon = new Polygon([[
  156. [$this->longitude + 0.005, $this->latitude + 0.005],
  157. [$this->longitude + 0.005, $this->latitude - 0.005],
  158. [$this->longitude - 0.005, $this->latitude - 0.005],
  159. [$this->longitude - 0.005, $this->latitude + 0.005],
  160. [$this->longitude + 0.005, $this->latitude + 0.005]
  161. ]]);
  162. $this->whaterPoints = new ArrayCollection();
  163. $this->ubications = new ArrayCollection();
  164. $this->establishments = new ArrayCollection();
  165. $this->createdAt = new \DateTime();
  166. $this->updatedAt = new \DateTime();
  167. $this->ownershipRequests = new ArrayCollection();
  168. }
  169. public function updateTownLocationData(
  170. string $name,
  171. string $slug = null,
  172. int $population = null,
  173. WhaterOrganization $whaterOrganization = null,
  174. DistributionNetwork $distributionNetwork = null
  175. ) {
  176. $this->name = $name;
  177. $this->slug = $slug;
  178. $this->population = $population;
  179. $this->whaterOrganization = $whaterOrganization;
  180. $this->distributionNetwork = $distributionNetwork;
  181. $this->updatedAt = new \DateTime();
  182. }
  183. public function editPolygon(
  184. Polygon $geometryPolygon
  185. ) {
  186. $this->geometryPolygon = $geometryPolygon;
  187. $this->record(new TownLocationPolygonWasUpdate($this));
  188. }
  189. public function editCenter(
  190. float $latitude = null,
  191. float $longitude = null
  192. ) {
  193. if ($latitude != null) {
  194. $this->latitude = $latitude;
  195. }
  196. if ($longitude != null) {
  197. $this->longitude = $longitude;
  198. }
  199. }
  200. public function updatesinacSynchonizationAt(\DateTime $sinacSynchonizationAt)
  201. {
  202. $this->sinacSynchonizationAt = $sinacSynchonizationAt;
  203. }
  204. /**
  205. * @return string
  206. */
  207. public function id(): string
  208. {
  209. return $this->uuid;
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function name(): string
  215. {
  216. return $this->name;
  217. }
  218. /**
  219. * @return string
  220. */
  221. public function townLocationType(): string
  222. {
  223. return $this->townLocationType;
  224. }
  225. /**
  226. * @return float|null
  227. */
  228. public function latitude(): ?float
  229. {
  230. return $this->latitude;
  231. }
  232. /**
  233. * @return float|null
  234. */
  235. public function longitude(): ?float
  236. {
  237. return $this->longitude;
  238. }
  239. /**
  240. * @return Polygon
  241. */
  242. public function geometryPolygon(): ?Polygon
  243. {
  244. return $this->geometryPolygon;
  245. }
  246. /**
  247. * @return LocationPolygon
  248. */
  249. public function locationPolygon(): ?LocationPolygon
  250. {
  251. if ($this->geometryPolygon() != null) {
  252. $pol = new LocationPolygon();
  253. $points = $this->geometryPolygon()->toArray();
  254. foreach ($points[0] as $point) {
  255. $lat = $point[1];
  256. $long = $point[0];
  257. $pol->addPoint(new Coordinate($lat, $long, Ellipsoid::createDefault()));
  258. }
  259. return $pol;
  260. }
  261. return null;
  262. }
  263. public function getGeoJsonPolygon(): ?string
  264. {
  265. if ($this->geometryPolygon() != null) {
  266. $geoJsonCoordinates = [];
  267. // Recorre los anillos (exterior + huecos si existen)
  268. foreach ($this->geometryPolygon()->getRings() as $ring) {
  269. $ringCoordinates = [];
  270. // Recorre los puntos del anillo
  271. foreach ($ring->getPoints() as $point) {
  272. $ringCoordinates[] = [$point->getLongitude(), $point->getLatitude()];
  273. }
  274. // Cierra el anillo si no está cerrado
  275. if ($ringCoordinates[0] !== $ringCoordinates[count($ringCoordinates) - 1]) {
  276. $ringCoordinates[] = $ringCoordinates[0];
  277. }
  278. // Añade el anillo a las coordenadas del GeoJSON
  279. $geoJsonCoordinates[] = $ringCoordinates;
  280. }
  281. // Construye el GeoJSON
  282. $geoJson = [
  283. "type" => "Polygon",
  284. "coordinates" => $geoJsonCoordinates
  285. ];
  286. return json_encode($geoJson, JSON_PRETTY_PRINT);
  287. }
  288. return null;
  289. }
  290. /**
  291. * @return string
  292. */
  293. public function slug(): ?string
  294. {
  295. return $this->slug;
  296. }
  297. /**
  298. * @return integer|null
  299. */
  300. public function population(): ?int
  301. {
  302. return $this->population;
  303. }
  304. /**
  305. * @return array
  306. */
  307. public function externalIds(): array
  308. {
  309. return $this->externalIds;
  310. }
  311. /**
  312. * @return Country
  313. */
  314. public function country(): Country
  315. {
  316. return $this->country;
  317. }
  318. /**
  319. * @return CountryArea|null
  320. */
  321. public function countryArea(): ?CountryArea
  322. {
  323. return $this->countryArea;
  324. }
  325. /**
  326. * @return Town
  327. */
  328. public function town(): Town
  329. {
  330. return $this->town;
  331. }
  332. /**
  333. * @return DistributionNetwork
  334. */
  335. public function distributionNetwork(): ?DistributionNetwork
  336. {
  337. return $this->distributionNetwork;
  338. }
  339. /**
  340. * @return Collection
  341. */
  342. public function whaterPoints(): Collection
  343. {
  344. return $this->whaterPoints;
  345. }
  346. /**
  347. * @return Collection
  348. */
  349. public function ubications(): Collection
  350. {
  351. return $this->ubications;
  352. }
  353. /**
  354. * @return Collection
  355. */
  356. public function establishments(): Collection
  357. {
  358. return $this->establishments;
  359. }
  360. /**
  361. * @return WhaterOrganization|null
  362. */
  363. public function whaterOrganization(): ?WhaterOrganization
  364. {
  365. return $this->whaterOrganization;
  366. }
  367. /**
  368. * @return \DateTime
  369. */
  370. public function createdAt(): \DateTime
  371. {
  372. return $this->createdAt;
  373. }
  374. /**
  375. * @return \DateTime
  376. */
  377. public function updatedAt(): \DateTime
  378. {
  379. return $this->updatedAt;
  380. }
  381. /**
  382. * @return Collection
  383. */
  384. public function ownershipRequests(): Collection
  385. {
  386. return $this->ownershipRequests;
  387. }
  388. /**
  389. * @return \DateTime
  390. */
  391. public function sinacSynchonizationAt(): ?\DateTime
  392. {
  393. return $this->sinacSynchonizationAt;
  394. }
  395. /**
  396. * @return bool
  397. */
  398. public function equals(TownLocation $town)
  399. {
  400. return $this->id() === $town->id();
  401. }
  402. }