Created
June 10, 2023 12:12
-
-
Save aziyan99/5bf5929c2bb3cced039f88f216ac39d9 to your computer and use it in GitHub Desktop.
simple geojson parser in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class GeoJSONType | |
{ | |
const PROVINCES = 'provinces'; | |
const REGENCIES = 'regencies'; | |
const DISTRICTS = 'districts'; | |
const VILLAGES = 'villages'; | |
const FEATURE_COLLECTION = 'FeatureCollection'; | |
} | |
abstract class GeoJSONProperty | |
{ | |
const NM_PROV = 'nmprov'; | |
const KD_PROV = 'kdprov'; | |
const NM_KAB = 'nmkab'; | |
const KD_KAB = 'kdkab'; | |
const NM_KEC = 'nmkec'; | |
const KD_KEC = 'kdkec'; | |
const NM_DES = 'nmdes'; | |
const KD_DES = 'kddes'; | |
} | |
class GeoJSONParser | |
{ | |
private $geoJSONString; | |
private $geoJSONData; | |
public function __construct($geoJSONString) | |
{ | |
$this->geoJSONString = $geoJSONString; | |
$this->geoJSONData = new stdClass(); | |
} | |
public function parseGeoJSON() | |
{ | |
$this->geoJSONData = json_decode($this->geoJSONString); | |
if ($this->geoJSONData->type !== 'FeatureCollection') { | |
throw new Exception('Invalid GeoJSON format. Expected FeatureCollection.'); | |
} | |
$features = $this->geoJSONData->features; | |
foreach ($features as $feature) { | |
$geometry = $feature->geometry; | |
$properties = $feature->properties; | |
$geoJSONType = $this->getGeoJSONType($properties); | |
$this->verifyRequiredProperties($geoJSONType, $properties); | |
$this->processGeometry($geoJSONType, $geometry, $properties); | |
} | |
} | |
private function getGeoJSONType($properties) | |
{ | |
if ( | |
property_exists($properties, GeoJSONProperty::NM_KEC) && | |
property_exists($properties, GeoJSONProperty::KD_KEC) && | |
property_exists($properties, GeoJSONProperty::NM_DES) && | |
property_exists($properties, GeoJSONProperty::KD_DES) | |
) { | |
return GeoJSONType::VILLAGES; | |
} elseif ( | |
property_exists($properties, GeoJSONProperty::NM_KEC) && | |
property_exists($properties, GeoJSONProperty::KD_KEC) && | |
property_exists($properties, GeoJSONProperty::NM_KAB) && | |
property_exists($properties, GeoJSONProperty::KD_KAB) | |
) { | |
return GeoJSONType::DISTRICTS; | |
} elseif ( | |
property_exists($properties, GeoJSONProperty::NM_KAB) && | |
property_exists($properties, GeoJSONProperty::KD_KAB) | |
) { | |
return GeoJSONType::REGENCIES; | |
} elseif ( | |
property_exists($properties, GeoJSONProperty::KD_PROV) && | |
property_exists($properties, GeoJSONProperty::NM_PROV) | |
) { | |
return GeoJSONType::PROVINCES; | |
} else { | |
throw new Exception('Unable to determine GeoJSON type.'); | |
} | |
} | |
private function verifyRequiredProperties($geoJSONType, $properties) | |
{ | |
$requiredProperties = []; | |
if ($geoJSONType === GeoJSONType::PROVINCES) { | |
$requiredProperties = [GeoJSONProperty::NM_PROV, GeoJSONProperty::KD_PROV]; | |
} elseif ($geoJSONType === GeoJSONType::REGENCIES) { | |
$requiredProperties = [GeoJSONProperty::NM_PROV, GeoJSONProperty::KD_PROV, GeoJSONProperty::NM_KAB, GeoJSONProperty::KD_KAB]; | |
} elseif ($geoJSONType === GeoJSONType::DISTRICTS) { | |
$requiredProperties = [GeoJSONProperty::NM_PROV, GeoJSONProperty::KD_PROV, GeoJSONProperty::NM_KAB, GeoJSONProperty::KD_KAB, GeoJSONProperty::NM_KEC, GeoJSONProperty::KD_KEC]; | |
} elseif ($geoJSONType === GeoJSONType::VILLAGES) { | |
$requiredProperties = [GeoJSONProperty::NM_PROV, GeoJSONProperty::KD_PROV, GeoJSONProperty::NM_KAB, GeoJSONProperty::KD_KAB, GeoJSONProperty::NM_KEC, GeoJSONProperty::KD_KEC, GeoJSONProperty::NM_DES, GeoJSONProperty::KD_DES]; | |
} | |
foreach ($requiredProperties as $property) { | |
if (!property_exists($properties, $property)) { | |
throw new Exception('Required property missing: ' . $property); | |
} | |
} | |
} | |
private function processGeometry($geoJSONType, $geometry, $properties) | |
{ | |
// Process the geometry based on the GeoJSON type | |
if ($geoJSONType === GeoJSONType::PROVINCES) { | |
$this->processProvincesGeometry($properties); | |
} elseif ($geoJSONType === GeoJSONType::REGENCIES) { | |
$this->processRegenciesGeometry($properties); | |
} elseif ($geoJSONType === GeoJSONType::DISTRICTS) { | |
$this->processDistrictsGeometry($properties); | |
} elseif ($geoJSONType === GeoJSONType::VILLAGES) { | |
$this->processVillagesGeometry($properties); | |
} | |
} | |
private function processProvincesGeometry($properties) | |
{ | |
$province = new stdClass(); | |
$province->{GeoJSONProperty::NM_PROV} = $properties->{GeoJSONProperty::NM_PROV}; | |
$province->{GeoJSONProperty::KD_PROV} = $properties->{GeoJSONProperty::KD_PROV}; | |
// TODO: Handle the province data as needed | |
// For example, you can store it in an array or perform any other desired operations | |
} | |
private function processRegenciesGeometry($properties) | |
{ | |
$regency = new stdClass(); | |
$regency->{GeoJSONProperty::NM_PROV} = $properties->{GeoJSONProperty::NM_PROV}; | |
$regency->{GeoJSONProperty::KD_PROV} = $properties->{GeoJSONProperty::KD_PROV}; | |
$regency->{GeoJSONProperty::NM_KAB} = $properties->{GeoJSONProperty::NM_KAB}; | |
$regency->{GeoJSONProperty::KD_KAB} = $properties->{GeoJSONProperty::KD_KAB}; | |
// TODO: Handle the regency data as needed | |
// For example, you can store it in an array or perform any other desired operations | |
} | |
private function processDistrictsGeometry($properties) | |
{ | |
$district = new stdClass(); | |
$district->{GeoJSONProperty::NM_PROV} = $properties->{GeoJSONProperty::NM_PROV}; | |
$district->{GeoJSONProperty::KD_PROV} = $properties->{GeoJSONProperty::KD_PROV}; | |
$district->{GeoJSONProperty::NM_KAB} = $properties->{GeoJSONProperty::NM_KAB}; | |
$district->{GeoJSONProperty::KD_KAB} = $properties->{GeoJSONProperty::KD_KAB}; | |
$district->{GeoJSONProperty::NM_KEC} = $properties->{GeoJSONProperty::NM_KEC}; | |
$district->{GeoJSONProperty::KD_KEC} = $properties->{GeoJSONProperty::KD_KEC}; | |
// TODO: Handle the district data as needed | |
// For example, you can store it in an array or perform any other desired operations | |
} | |
private function processVillagesGeometry($properties) | |
{ | |
$village = new stdClass(); | |
$village->{GeoJSONProperty::NM_PROV} = $properties->{GeoJSONProperty::NM_PROV}; | |
$village->{GeoJSONProperty::KD_PROV} = $properties->{GeoJSONProperty::KD_PROV}; | |
$village->{GeoJSONProperty::NM_KAB} = $properties->{GeoJSONProperty::NM_KAB}; | |
$village->{GeoJSONProperty::KD_KAB} = $properties->{GeoJSONProperty::KD_KAB}; | |
$village->{GeoJSONProperty::NM_KEC} = $properties->{GeoJSONProperty::NM_KEC}; | |
$village->{GeoJSONProperty::KD_KEC} = $properties->{GeoJSONProperty::KD_KEC}; | |
$village->{GeoJSONProperty::NM_DES} = $properties->{GeoJSONProperty::NM_DES}; | |
$village->{GeoJSONProperty::KD_DES} = $properties->{GeoJSONProperty::KD_DES}; | |
// TODO: Handle the village data as needed | |
// For example, you can store it in an array or perform any other desired operations | |
} | |
public function getDynamicGeoJSON() | |
{ | |
$dynamicGeoJSON = new stdClass(); | |
$dynamicGeoJSON->type = $this->geoJSONData->type; | |
$dynamicGeoJSON->properties = []; | |
$features = $this->geoJSONData->features; | |
foreach ($features as $feature) { | |
$properties = $feature->properties; | |
$geoJSONType = $this->getGeoJSONType($properties); | |
$data = new stdClass(); | |
if ($geoJSONType === GeoJSONType::PROVINCES) { | |
$data->{GeoJSONProperty::NM_PROV} = $properties->{GeoJSONProperty::NM_PROV}; | |
$data->{GeoJSONProperty::KD_PROV} = $properties->{GeoJSONProperty::KD_PROV}; | |
} elseif ($geoJSONType === GeoJSONType::REGENCIES) { | |
$data->{GeoJSONProperty::NM_KAB} = $properties->{GeoJSONProperty::NM_KAB}; | |
$data->{GeoJSONProperty::KD_KAB} = $properties->{GeoJSONProperty::KD_KAB}; | |
} elseif ($geoJSONType === GeoJSONType::DISTRICTS) { | |
$data->{GeoJSONProperty::NM_KEC} = $properties->{GeoJSONProperty::NM_KEC}; | |
$data->{GeoJSONProperty::KD_KEC} = $properties->{GeoJSONProperty::KD_KEC}; | |
} elseif ($geoJSONType === GeoJSONType::VILLAGES) { | |
$data->{GeoJSONProperty::NM_DES} = $properties->{GeoJSONProperty::NM_DES}; | |
$data->{GeoJSONProperty::KD_DES} = $properties->{GeoJSONProperty::KD_DES}; | |
} | |
$dynamicGeoJSON->properties[] = $data; | |
} | |
return $dynamicGeoJSON; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment