4

I am trying to add GeoJSON data into MongoDB, but some of the GeoJSON shapes are failing due to the following error:

com.mongodb.MongoWriteException: Can't extract geo keys: ...Loop 1 shares more than one vertex with its parent loop 0

Here is an example GeoJSON for which this occurs.

enter image description here

Specifically, the error is caused by the two points highlighted in the following close up image. These two points belong to both the shown inner linear ring as well as the exterior ring, and this is what is causing the error.

enter image description here

So here is my question: I am unsure how I can detect and fix this GeoJSON automatically. I would imagine that this one Polygon would need to be converted into a MultiPolygon with two pieces (as opposed to a polygon with a hole, as it is now).

Or, if anyone knows of any tools/methods to make GeoJSON data compliant with MongoDB that would be good.

Here is the full GeoJSON for the shape, where the exterior loop is item in the coordinates array, and the inner loop causing the error is the last item.

{
    "id": 110811,
    "osm_type": "relation",
    "type": "Feature",
    "name": "Litchfield Park",
    "properties": {
        "name": "Litchfield Park",
        "place": "city",
        "source": "TIGER/Line® 2008 Place Shapefiles (http://www.census.gov/geo/www/tiger/)",
        "boundary": "administrative",
        "admin_level": "8",
        "tiger:reviewed": "no"
    },
    "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
                [
                    [-112.323993, 33.5039334],
                    [-112.32435799999999, 33.50408899999999],
                    [-112.327754, 33.504048999999995],
                    [-112.327773, 33.502589],
                    [-112.327783, 33.501939],
                    [-112.32785, 33.497080999999994],
                    [-112.32786999999999, 33.49552800000001],
                    [-112.32788, 33.49486099999999],
                    [-112.327901, 33.493253],
                    [-112.32825799999999, 33.493253],
                    [-112.3329832, 33.493306700000005],
                    [-112.335622, 33.493299],
                    [-112.336288, 33.49330200000001],
                    [-112.336288, 33.493488],
                    [-112.336801, 33.493486999999995],
                    [-112.34086900000001, 33.493482],
                    [-112.34322700000001, 33.493491],
                    [-112.34386, 33.493299],
                    [-112.34452000000002, 33.49310200000001],
                    [-112.34522600000001, 33.49289],
                    [-112.34600540000001, 33.492263900000005],
                    [-112.34772330000001, 33.4910055],
                    [-112.3486354, 33.4903811],
                    [-112.34913360000002, 33.4900443],
                    [-112.3503794, 33.4892932],
                    [-112.35093009999999, 33.4889884],
                    [-112.35122640000002, 33.488814],
                    [-112.35173169999999, 33.488559],
                    [-112.35256779999999, 33.4881515],
                    [-112.353533, 33.487733999999996],
                    [-112.3545854, 33.487312700000004],
                    [-112.35478839999999, 33.48724789999999],
                    [-112.3558504, 33.48684639999999],
                    [-112.3567109, 33.48658439999999],
                    [-112.35810099999999, 33.48628099999999],
                    [-112.35887499999998, 33.486032],
                    [-112.35918, 33.486002],
                    [-112.36033699999999, 33.48595],
                    [-112.362315, 33.48601],
                    [-112.362413, 33.487824999999994],
                    [-112.362425, 33.487936999999995],
                    [-112.36246999999999, 33.488605],
                    [-112.362433, 33.489405999999995],
                    [-112.36243599999999, 33.490203],
                    [-112.36241199999999, 33.491005],
                    [-112.362423, 33.491795],
                    [-112.36245, 33.493412000000006],
                    [-112.362467, 33.493505000000006],
                    [-112.362514, 33.493480999999996],
                    [-112.366826, 33.493553000000006],
                    [-112.36933799999998, 33.49353000000001],
                    [-112.37179779999998, 33.4934689],
                    [-112.371945, 33.493509],
                    [-112.37508899999999, 33.493483999999995],
                    [-112.37508699999998, 33.493613],
                    [-112.375283, 33.493611],
                    [-112.37527599999999, 33.495253000000005],
                    [-112.37526999999999, 33.4966],
                    [-112.37526999999999, 33.496704],
                    [-112.37521699999999, 33.497033],
                    [-112.37544399999999, 33.497034],
                    [-112.37552799999999, 33.498924],
                    [-112.37983499999999, 33.498924],
                    [-112.37983399999999, 33.503718000000006],
                    [-112.37982699999999, 33.508189],
                    [-112.375215, 33.508184],
                    [-112.37498799999999, 33.508184],
                    [-112.37172199999999, 33.508201],
                    [-112.36935199999999, 33.50821200000001],
                    [-112.366981, 33.508225],
                    [-112.3669724, 33.50821390000001],
                    [-112.362921, 33.508245],
                    [-112.36120600000001, 33.508249],
                    [-112.35982879999999, 33.5082656],
                    [-112.35896999999999, 33.508276],
                    [-112.358268, 33.508283999999996],
                    [-112.35807099999998, 33.508273],
                    [-112.35778599999999, 33.508257],
                    [-112.3578185, 33.5082857],
                    [-112.357033, 33.508237],
                    [-112.356505, 33.508235000000006],
                    [-112.353686, 33.508159000000006],
                    [-112.349502, 33.508046],
                    [-112.34901900000001, 33.508034],
                    [-112.34836999999999, 33.50801500000001],
                    [-112.346907, 33.507975],
                    [-112.345176, 33.507926999999995],
                    [-112.34322600000002, 33.507872],
                    [-112.342895, 33.50786299999999],
                    [-112.342428, 33.50784899999999],
                    [-112.340883, 33.50780699999999],
                    [-112.340884, 33.508026],
                    [-112.3420167, 33.5080706],
                    [-112.342433, 33.508086999999996],
                    [-112.34323930000001, 33.50810500000001],
                    [-112.34516400000001, 33.508148000000006],
                    [-112.349486, 33.508294],
                    [-112.35068799999999, 33.508345000000006],
                    [-112.350743, 33.508451],
                    [-112.353687, 33.508564],
                    [-112.358069, 33.508553000000006],
                    [-112.35806699999999, 33.50873800000001],
                    [-112.353687, 33.508791],
                    [-112.35316900000001, 33.508769],
                    [-112.349476, 33.508618000000006],
                    [-112.34941900000001, 33.508618000000006],
                    [-112.34515300000001, 33.508419],
                    [-112.342421, 33.50831900000001],
                    [-112.340886, 33.508349],
                    [-112.33733, 33.5082],
                    [-112.335424, 33.50831000000001],
                    [-112.33279999999999, 33.508381],
                    [-112.33271799999999, 33.510760999999995],
                    [-112.33262300000001, 33.512799],
                    [-112.332641, 33.513835],
                    [-112.332611, 33.51462299999999],
                    [-112.332469, 33.51465099999999],
                    [-112.33176999999999, 33.51469899999999],
                    [-112.33128900000001, 33.51469399999999],
                    [-112.32992, 33.51468499999999],
                    [-112.329296, 33.51469899999999],
                    [-112.32825199999999, 33.51468499999999],
                    [-112.3241173, 33.51467939999999],
                    [-112.3239957, 33.5043606],
                    [-112.323993, 33.5039334]
                ],
                [
                    [-112.324175, 33.51456099999999],
                    [-112.325555, 33.514655999999995],
                    [-112.32841799999998, 33.51442899999999],
                    [-112.32935499999999, 33.51442899999999],
                    [-112.329967, 33.514450999999994],
                    [-112.331288, 33.514461999999995],
                    [-112.331855, 33.514461999999995],
                    [-112.332469, 33.51444599999999],
                    [-112.332469, 33.51383],
                    [-112.332469, 33.512862999999996],
                    [-112.33247499999999, 33.512839],
                    [-112.33249799999999, 33.512813],
                    [-112.3325195, 33.51219890000001],
                    [-112.332565, 33.510898999999995],
                    [-112.332628, 33.50813000000001],
                    [-112.335753, 33.508013000000005],
                    [-112.33733, 33.508021],
                    [-112.340745, 33.507962],
                    [-112.34072400000001, 33.50780399999999],
                    [-112.340883, 33.50780699999999],
                    [-112.340883, 33.507698999999995],
                    [-112.340712, 33.507709],
                    [-112.339073, 33.507656999999995],
                    [-112.337862, 33.507517],
                    [-112.337855, 33.507298999999996],
                    [-112.33765900000002, 33.506749],
                    [-112.337857, 33.50669],
                    [-112.33777099999999, 33.506496999999996],
                    [-112.338185, 33.50645],
                    [-112.33894199999999, 33.506255],
                    [-112.340882, 33.506334],
                    [-112.34088, 33.504540999999996],
                    [-112.34087799999999, 33.502598000000006],
                    [-112.340875, 33.499307],
                    [-112.340873, 33.497106],
                    [-112.34087099999999, 33.495249],
                    [-112.34087099999999, 33.49499],
                    [-112.33878899999999, 33.494963],
                    [-112.338206, 33.495243],
                    [-112.33727900000001, 33.495697],
                    [-112.336288, 33.494925],
                    [-112.336288, 33.493488],
                    [-112.332267, 33.493494],
                    [-112.33151500000001, 33.493473],
                    [-112.32825799999999, 33.493387],
                    [-112.32820799999999, 33.493982],
                    [-112.32820099999999, 33.494717],
                    [-112.32818999999999, 33.49535900000001],
                    [-112.328126, 33.49633000000001],
                    [-112.328154, 33.496862],
                    [-112.32808599999998, 33.497473],
                    [-112.32809699999999, 33.498209],
                    [-112.32803299999999, 33.499086],
                    [-112.32800599999999, 33.499883999999994],
                    [-112.32803599999998, 33.500665999999995],
                    [-112.32802999999998, 33.501526999999996],
                    [-112.32803999999999, 33.502167],
                    [-112.327946, 33.504215],
                    [-112.324315, 33.50434],
                    [-112.324304, 33.506460999999994],
                    [-112.324349, 33.507514],
                    [-112.324277, 33.513456999999995],
                    [-112.32426070000001, 33.5136339],
                    [-112.324175, 33.51456099999999]
                ]
            ]
        ]
    }
}

1 Answer 1

3

It should be possible to make your GeoJSON file valid with ogr2ogr http://www.gdal.org/ogr2ogr.html by using OGR SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html which supports all the Spatialite functions https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html includind MakeValid. Unfortunately the function is very new in SpatiaLite and using it would require a special GDAL version that is compiled with SpatiaLite development version from the trunk.

The usage would be close to this untested command:

ogr2ogr -f GeoJSON -dialect SQLite -sql "SELECT MakeValid(geometry),name,place,source,boundary,admin_level from OGRGeoJSON" corrected.json invalid.json

I think I could fix your invalid GeoJSON but it took awhile to find a program and format that opens the invalid polygon.

  • Convert invalid GeoJSON to OpenJUMP JML format. This was necessary because OpenJUMP did not open the invalid GeoJSON directly but it did not complain about invalid JML

ogr2ogr -f jml invalid.jml invalid.json

  • Open invalid.json with OpenJUMP

  • Correct the geometries by using the tool from menu Tools - QA - Make Geometries Valid

  • Save corrected layer in GeoJSON format with OpenJUMP

6
  • Thanks for the response! I'm trying to get this working with the SQLite dialect. What would the table name in the query would be?
    – havenwang
    Commented May 5, 2017 at 21:46
  • Usually the layer in GeoJSON has the same name as the base of the file name but it is always good to check it with ogrinfo. If your SpatiaLite is too old you will see a message about missing function later.
    – user30184
    Commented May 6, 2017 at 6:53
  • I tried the following to get the available tables ogrinfo -dialect SQLite -sql "SELECT * FROM sqlite_master where type='table'" invalid.json. This showed 20 tables, some examples are tables with names: spatialite_history, geometry_columns, spatial_ref_sys_aux, views_geometry_columns...However when I try to view the contents of the tables to find the geometry objects, they don't appear to be there. Am I missing something?
    – havenwang
    Commented May 8, 2017 at 18:51
  • Not that complicated. As documented in gdal.org/ogrinfo.html "If no layer names are passed then ogrinfo will report a list of available layers (and their layer wide geometry type)." Just do ogrinfo your_geojson.json.
    – user30184
    Commented May 8, 2017 at 18:58
  • Awesome, this ended up working: ogr2ogr -f GeoJSON -dialect SQLite -sql "SELECT MakeValid(geometry),name,place,source,boundary,admin_level from OGRGeoJSON" corrected.json invalid.json
    – havenwang
    Commented May 8, 2017 at 20:24

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.