twpayne / go-geom

Package geom implements efficient geometry types for geospatial applications.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong Number of Polygons in Multipolygon

cajonKA opened this issue · comments

When I unmarshal the following GeoJson Multipolygon and later call NumPolygons on it, I would expect to get 2 as result, but the result is 1, because endss is [[10 20]] instead of [[10] [20]]
{"type":"MultiPolygon","coordinates":[[[[0,35],[0,40],[10,40],[10,35],[0,35]],[[6,48],[6,51],[10,51],[10,48],[6,48]]]]}

Is this a bug or am I missing some important point ?

Here's the hierarchy:

  • A point is []float64.
  • A ring is a sequence of points, i.e. [][]float64.
  • A polygon is a sequence of rings, the first is the outer ring and any more are inner rings, i.e. [][][]float64. The outer ring is the boundary and inner rings are holes.
    A multipolygon is a collection of polygons, i.e. [][][][]float64.

What you have in your GeoJSON is a multipolygon containing a single polygon with two rings:

[
  [ // First polygon
    [[0,35],[0,40],[10,40],[10,35],[0,35]], // Outer ring
    [[6,48],[6,51],[10,51],[10,48],[6,48]]  // Inner ring
  ]
]

You probably want coordinates to be

[
  [ // First poly
    [[0,35],[0,40],[10,40],[10,35],[0,35]] // Outer ring
  ],
  [ // Second poly
    [[6,48],[6,51],[10,51],[10,48],[6,48]] // Outer ring
  ]
]

Please re-open this bug if needed.

So its a matter of clinching the numbers together ? I forgot the inner clinch, so I had this outer and inner ring construct. Thanks for leading me in the right way.

Just one simple question which is not specific to your lib, but maybe you can still enlighten me.
How can a inner and outer ring exist, if the two polys are far away from each other and don't intersect ?

go-geom follows the OGC specifications here. Just because a polygon can be represented, it does not mean that it is valid. See section 6.1.11.1 of the OpenGIS Implementation Specification for Geographic information - Simple feature access - Part 1: Common architecture for more information.

I was not aware of this. Thanks for your help and for this really helpful lib.