(no commit message)
authorChristian Weiske <cweiske@cweiske.de>
Thu, 15 Mar 2018 20:45:30 +0000 (21:45 +0100)
committerwww-cweiske <www-cweiske@localhost.localdomain>
Thu, 15 Mar 2018 20:45:30 +0000 (21:45 +0100)
arcgis-to-geojson.php [new file with mode: 0644]
combine-geojson.php [new file with mode: 0644]

diff --git a/arcgis-to-geojson.php b/arcgis-to-geojson.php
new file mode 100644 (file)
index 0000000..073c26a
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Convert a ArcGis web recordset polygon to a geojson polygon feature.
+ *
+ * Validate the generated JSON with http://geojsonlint.com/
+ *
+ * Dependency installation:
+ * $ composer require php-coord/php-coord
+ *
+ * @author Christan Weiske <cweiske@cweiske.de>
+ */
+require_once __DIR__ . '/vendor/autoload.php';
+
+function err($msg)
+{
+    echo $msg . "\n";
+    exit(1);
+}
+
+if ($argc < 2) {
+    err("convert.php <esriGeoMetryFile.json>");
+}
+
+$file = $argv[1];
+$data = json_decode(file_get_contents($file));
+
+if (!isset($data->RecordSet->geometryType)
+    || $data->RecordSet->geometryType != 'esriGeometryPolygon'
+) {
+    err('No esriGeometryPolygon');
+}
+
+$sRefs = [
+    25833 => ['N', 33]
+];
+$sRef = $data->RecordSet->spatialReference->wkid;
+if (!isset($sRefs[$sRef])) {
+    err('Unknown spatial reference EPSG ' . $sRef);
+}
+list($latZone, $lngZone) = $sRefs[$sRef];
+
+$geoJson = [
+    'type'       => 'Feature',
+    'title'      => basename($file, '.json'),
+    'properties' => null,
+    'geometry'   => [
+        'type'        => 'Polygon',
+        'coordinates' => [],
+    ],
+];
+foreach ($data->RecordSet->features as $feature) {
+    foreach ($feature->geometry->rings as $ring) {
+        $coords = [];
+        foreach ($ring as $point) {
+            $utm = new \PHPCoord\UTMRef(
+                $point[0], $point[1], 0, $latZone, $lngZone
+            );
+            $ll = $utm->toLatLng();
+            $coords[] = [$ll->getLng(), $ll->getLat()];
+        }
+
+        $geoJson['geometry']['coordinates'][] = $coords;
+    }
+}
+
+echo json_encode($geoJson, JSON_PRETTY_PRINT) . "\n";
+
+/*
+$UTMRef = new \PHPCoord\UTMRef(350531.8, 5689109.2, 0, 'N', 33);
+$LatLng = $UTMRef->toLatLng();
+
+echo (string) $UTMRef . "\n";
+echo (string) $LatLng . "\n";
+*/
+?>
diff --git a/combine-geojson.php b/combine-geojson.php
new file mode 100644 (file)
index 0000000..2df9115
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Takes several geojson files and combines them into a single one.
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ */
+function err($msg)
+{
+    echo $msg . "\n";
+    exit(1);
+}
+
+if ($argc < 2) {
+    err("combine-geojson.php <files>");
+}
+
+$files = $argv;
+array_shift($files);
+
+$combined = [
+    'type'     => 'FeatureCollection',
+    'features' => [],
+];
+
+foreach ($files as $file) {
+    $combined['features'][] = json_decode(file_get_contents($file));
+}
+
+echo json_encode($combined, JSON_PRETTY_PRINT) . "\n";
+?>