spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

GeoJsonPolygonconvertor does not convert inner rings.

Open lennertdefeyter opened this issue 3 years ago • 3 comments

Hi,

I noticed that the GeojsonPolygonconvertor does not convert the inner rings. If you want to convert a polygon with holes. It only looks at the outer ring.

This is very apparent in the code in line 759 of GeoConverters.java. static GeoJsonPolygon toGeoJsonPolygon(List dbList) { return new GeoJsonPolygon(toListOfPoint((List) dbList.get(0))); }

As you can see, it only looks at the first item in the list of rings (e.g. the outer ring).

I've already fixed it with a custom convertor (see attachment, although this is for multipolygons), but it might be nice to have this in the core.

I think that in order to fix it, the toGeoJsonPolygon(List dbList) function in GeoConverters.java needs to change to:

static GeoJsonPolygon toGeoJsonPolygon(List dbList) { Iterator<GeoJsonLineString> it = ((List) dbList).iterator(); GeoJsonPolygon g = new GeoJsonPolygon(toListOfPoint((List) it.next())); while (it.hasNext()) g = g.withInnerRing(toListOfPoint((List) it.next())); return g; }

GeoJsonMultiPolygonDeserializer.java.txt .

lennertdefeyter avatar Jul 08 '22 07:07 lennertdefeyter

Which version are you using? It would be great if you could please take the time to provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.

christophstrobl avatar Jul 20 '22 13:07 christophstrobl

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Jul 28 '22 04:07 spring-projects-issues

Hi @christophstrobl ,

Sorry for the delay. This is version 2.7.2.

Attached is a zip that should show the problem.

it inserts a polygon correctly with a donut hole in the center: outer ring is a box with bounds: 0,0 -> 10,10 . Inner ring is a box 4,4 -> 6,6

It is stored correctly in the database. But when querying it clearly has only the outer ring: image

mongodbdemo.zip

Thank you for your help.

lennertdefeyter avatar Jul 28 '22 16:07 lennertdefeyter

Sorry for long silence - good catch! Thank you for the reproducer!

christophstrobl avatar Sep 08 '22 07:09 christophstrobl

@mp911de Hi,

Thank you in advance for the help! Unfortunately this will only fix the issue if it has only one donut hole. A lot of polygons have multiple, for example, if you have a layer for all countries in the world, Italy would have holes for San Marino and Vatican city.

For an example on a full fix, you can reference the deserializer I added in the original post. Otherwise I'll try to make my own PR this weekend.

Kind regards and again thank you for your time,

Lennert

lennertdefeyter avatar Sep 14 '22 12:09 lennertdefeyter

Thanks for reviewing. We have a release planned for Monday, so the earlier we get a patch the more likely we can include it. Am 14. Sept. 2022, 14:24 +0200 schrieb lennertdefeyter @.***>:

@mp911de Hi, Thank you in advance for the help! Unfortunately this will only fix the issue if it has only one donut hole. A lot of polygons have multiple, for example, if you have a layer for all countries in the world, Italy would have holes for San Marino and Vatican city. For an example on a full fix, you can reference the deserializer I added in the original post. Otherwise I'll try to make my own PR this weekend. Kind regards and again thank you for your time, Lennert — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

mp911de avatar Oct 11 '22 08:10 mp911de