Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoJSON MultiPolygon with multiple holes

Below I have what I'd expect is a way to create a GeoJSON MultiPolygon object with one polygon in it which has two "holes".

When I use the service http://geojson.io/ to validate this object, it returns with an error each element in a position must be a number and it does not render, however if I remove the "holes" nest, removing one of them then it works.

I'm looking for a way to describe a MultiPolygon where the polygons can have multiple holes.

I'm not looking for a way in code to create a polygon with holes.

I'm looking for a way to use the GeoJSON spec to represent MultiPolygons with multiple holes.

enter image description here

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -73.98114904754641,
          40.7470284264813
        ],
        [
          -73.98314135177611,
          40.73416844413217
        ],
        [
          -74.00538969848634,
          40.734314779027144
        ],
        [
          -74.00479214294432,
          40.75027851544338
        ],
        [
          -73.98114904754641,
          40.7470284264813
        ]
      ],
      [
        [
          [
            -73.99818643920906,
            40.74550031602355
          ],
          [
            -74.00298643920905,
            40.74550031602355
          ],
          [
            -74.00058643920897,
            40.74810024102966
          ],
          [
            -73.99818643920906,
            40.74550031602355
          ]
        ],
        [
          [
            -73.98917421691903,
            40.73646098717515
          ],
          [
            -73.99397421691901,
            40.73646098717515
          ],
          [
            -73.99157421691893,
            40.739061265535696
          ],
          [
            -73.98917421691903,
            40.73646098717515
          ]
        ]
      ]
    ]
  ]
}
like image 471
ThomasReggi Avatar asked Apr 26 '17 21:04

ThomasReggi


People also ask

What is a Multipolygon in GeoJSON?

A multiPolygon is an array of Polygon coordinate arrays. This adheres to the RFC 7946 internet standard when serialized into JSON. When deserialized, this class becomes an immutable object which should be initiated using its static factory methods.

What is the difference between polygon and Multipolygon?

A MULTIPOLYGON is a collection of Polygons. MultiPolygons are useful for gathering a group of Polygons into one geometry. For example, you may want to gather the Polygons denoting a group of properties in a particular municipality.

Is GeoJSON Lat Long or Long LAT?

Nothing is in the wrong order. Leaflet uses lat-lng (or northing-easting ) whereas GeoJSON uses lng-lat (or easting-northing ).

How do you draw a polygon in GeoJSON?

Select the Draw a polygon button, which similar to drawing a polyline, except that you need to complete the feature by making your final point at the same location as your initial point.


2 Answers

This is how it works:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      {polygon},
      {hole},
      {hole},
      {hole}
    ]
  ]
}

Not like this:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      {polygon},
      [
        {hole},
        {hole},
        {hole}
      ]
    ]
  ]
}

Here's an example!

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -47.900390625,
          -14.944784875088372
        ],
        [
          -51.591796875,
          -19.91138351415555
        ],
        [
          -41.11083984375,
          -21.309846141087192
        ],
        [
          -43.39599609375,
          -15.390135715305204
        ],
        [
          -47.900390625,
          -14.944784875088372
        ]
      ],
      [
        [
          -46.6259765625,
          -17.14079039331664
        ],
        [
          -47.548828125,
          -16.804541076383455
        ],
        [
          -46.23046874999999,
          -16.699340234594537
        ],
        [
          -45.3515625,
          -19.31114335506464
        ],
        [
          -46.6259765625,
          -17.14079039331664
        ]
      ],
      [
        [
          -44.40673828125,
          -18.375379094031825
        ],
        [
          -44.4287109375,
          -20.097206227083888
        ],
        [
          -42.9345703125,
          -18.979025953255267
        ],
        [
          -43.52783203125,
          -17.602139123350838
        ],
        [
          -44.40673828125,
          -18.375379094031825
        ]
      ]
    ]
  ]
}
like image 68
ThomasReggi Avatar answered Sep 18 '22 01:09

ThomasReggi


For your example in fact it's not really a MultiPolygon (in the sense of geoJSON) but a simple Polygon (with a single outer ring and multiple inner rings for the holes). Note the difference with Multipolygons in OSM (which represents them as a relation containing ways, and whose first and last node should be merged to the same "node" element (something that does not exist in geoJSON where they are unified only by the fact that the two nodes have the same coordinates, but will in reality be automatically closed by an additional segment for "Polygon" and "MultiPolygon" types of GeoJSON)

Note that when you import a geoJSON in OSM editors (such as JOSM) they will be imported with separate nodes for the first and last node, even if they have the same coordinates - you need to use the JOSM validator to detect superposed nodes and merge them after the import in JOSM but before submission to OSM.

But in scripts or general use of geoJSON, all rings (arrays of coordinate pairs) in a "type":"Polygon" or members of a "type":"Polygon" are not required to include the same coordinates for the last node as the first node, because it is implicit (but it is still recommended to add this duplicate node for compatibility). Such closure of rings is implicit for "Polygon" and "MultiPolygon" (as they represent surfaces), but not for "Polyline" and "MultiPolyline" (as they represent curves) where you still need to include twice the same coordinates for the first and last node to get closed curves.

To represent an OSM "multipolygon" with multiple "outer" rings, you have to include several "[ {outer},{inner*} ]" in the main array of coordinates for the geoJSON "MultiPolygon" type, i.e.

{"type":"MultiPolygon", "coordinates":[
  [
    [[x0,y0], [x1,y1], ... [x0,y0]], /*outer1*/
    [[x0,y0], [x1,y1], ... [x0,y0]], /*inner1, optional*/
    [[x0,y0], [x1,y1], ... [x0,y0]], /*inner2, optional*/
  ],[
    [[x0,y0], [x1,y1], ... [x0,y0]], /*outer2*/
  ],...,[
    [[x0,y0], [x1,y1], ... [x0,y0]], /*outer3*/
  ],[
    [[x0,y0], [x1,y1], ... [x0,y0]], /*outer4*/
  ]
]}

So for your example, the solution is:

{"type":"Polygon", "coordinates":[
  [[x0,y0], [x1,y1], [x2,y2], [x3,y3], [x0,y0]], /*outer1*/
  [[x4,y4], [x5,y5], [x6,y6], [x4,y4]],          /*inner1*/
  [[x7,y7], [x8,y8], [x9,y9], [x7,y7]]           /*inner2*/
]}

If you had several outer rings only (possibly overlapping to create an union of surfaces, but this is not recommended) it would need to be a MultiPolygon, and here you would get no "holes":

{"type":"MultiPolygon", "coordinates":[
  [[[x0,y0], [x1,y1], [x2,y2], [x3,y3], [x0,y0]]], /*outer1*/
  [[[x4,y4], [x5,y5], [x6,y6], [x4,y4]]],          /*outer2*/
  [[[x7,y7], [x8,y8], [x9,y9], [x7,y7]]]           /*outer3*/
]}

Note there's one less level of [square braces] because we can use "Polygon" here instead of a Multipolygon that would contain only one member in your example.

like image 26
verdy_p Avatar answered Sep 17 '22 01:09

verdy_p