1

I have a multi-level GeoJSON file and I need to create a new geometry field within it that is a copy of the old geometry field but with a different key name. The new geometry field would exist in the properties section of the GeoJSON. I've tried a few different methods in Python but I keep getting an invalid GeoJSON. Below is an example GeoJSON file.

{"type": "FeatureCollection",
 "crs": {
     "type": "name", 
     "properties": {
         "name": "EPSG:4269"}},
 "features": [{
      'type': 'Feature',
      'id': 1,
      'geometry': {'type': 'Polygon',
       'coordinates': [[[-122.49195406899997, 37.703511726000045],
         [-122.48671922899996, 37.703525668000054],
         [-122.48671886499994, 37.703578453000034]]]},
      'properties': {'OBJECTID': 1,
       'key1': 'item1',
       'key2': 87,
       'shape_Length': 0.025794070246599403,
       'shape_Area': 3.7520109384232215e-05,
       'Polyline_Count': 22,
       'SUM_shape_Length': 0.0158352870070439,
       'Total_Miles': 1.03803516533877}},
    {'type': 'Feature',
      'id': 2,
      'geometry': {'type': 'Polygon',
       'coordinates': [[[-122.47670391799994, 37.70354855800008],
         [-122.46908111099998, 37.70356779300005],
         [-122.47670391799994, 37.70354855800008]]]},
      'properties': {'OBJECTID': 2,
       'key1': 'item1',
       'key2': 176,
       'shape_Length': 0.02483662298661403,
       'shape_Area': 3.551651638576507e-05,
       'Polyline_Count': 45,
       'SUM_shape_Length': 0.0263386544389041,
       'Total_Miles': 1.56305232720018}}]
 }
1

1 Answer 1

3

Your input GeoJSON is invalid (copy paste it in https://geojsonlint.com). You can't expect a valid GeoJSON output from a bad input. So, first, you should replace all single quotes with double quotes. crs property is not in the specification so it has been removed. You also need to close the polygon: the end point should be the same as the start point (see the diff content that shows changed made).

--- a/tmp/after-correction.json
+++ b/tmp/before-correction.json
@@ -1,11 +1,5 @@
 {
   "type": "FeatureCollection",
-  "crs": {
-    "type": "name",
-    "properties": {
-      "name": "EPSG:4269"
-    }
-  },
   "features": [
     {
       "type": "Feature",
@@ -25,6 +19,10 @@
             [
               -122.48671886499994,
               37.703578453000034
+            ],
+            [
+              -122.49195406899997,
+              37.703511726000045
             ]
           ]
         ]
@@ -55,6 +53,10 @@
               -122.46908111099998,
               37.70356779300005
             ],
+            [

The resulting JSON should be

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.49195406899997,
              37.703511726000045
            ],
            [
              -122.48671922899996,
              37.703525668000054
            ],
            [
              -122.48671886499994,
              37.703578453000034
            ],
            [
              -122.49195406899997,
              37.703511726000045
            ]
          ]
        ]
      },
      "properties": {
        "OBJECTID": 1,
        "key1": "item1",
        "key2": 87,
        "shape_Length": 0.025794070246599403,
        "shape_Area": 3.7520109384232215e-05,
        "Polyline_Count": 22,
        "SUM_shape_Length": 0.0158352870070439,
        "Total_Miles": 1.03803516533877
      }
    },
    {
      "type": "Feature",
      "id": 2,
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.47670391799994,
              37.70354855800008
            ],
            [
              -122.46908111099998,
              37.70356779300005
            ],
            [
              -122.47670391799994,
              37.70354855800008
            ],
            [
              -122.47670391799994,
              37.70354855800008
            ]
          ]
        ]
      },
      "properties": {
        "OBJECTID": 2,
        "key1": "item1",
        "key2": 176,
        "shape_Length": 0.02483662298661403,
        "shape_Area": 3.551651638576507e-05,
        "Polyline_Count": 45,
        "SUM_shape_Length": 0.0263386544389041,
        "Total_Miles": 1.56305232720018
      }
    }
  ]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.