Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometry.union fails with TopologyException : found non-noded intersection #657

Closed
hxhowl opened this issue Dec 24, 2020 · 6 comments
Closed

Comments

@hxhowl
Copy link

hxhowl commented Dec 24, 2020

My test code like this:

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;

public class LocalMapTest {

    private static final String area3332 = "MULTIPOLYGON (((120.65259965054962 31.285279777143465, 120.67974663557145 31.288251142146198, 120.68831455911376 31.28918923620673, 120.70794961491976 31.282636365231323, 120.76374886330346 31.283832422390343, 120.78566214063716 31.282849157114605, 120.78727475541889 31.282211626764653, 120.7870154283358 31.28112971510409, 120.78159715214149 31.281396505875275, 120.7675993122942 31.282369818420804, 120.76620314875765 31.281048292763057, 120.7622268383381 31.282310090290256, 120.75562997396135 31.281939054121096, 120.74886389782327 31.28171655444499, 120.74540558497365 31.28169741706209, 120.73917835062895 31.28125748233043, 120.73308266356383 31.281259760653278, 120.70952760230696 31.279938436718577, 120.70172330921004 31.281048754102418, 120.690769367383 31.283502308101333, 120.68806834503596 31.285431202279767, 120.67784877635694 31.28510197877543, 120.66475600163066 31.284907044197734, 120.65301591965225 31.28423244481776, 120.65273638403228 31.28526053475525, 120.6525866745773 31.28529679390149, 120.6525866745773 31.28529679390149, 120.65259965054962 31.285279777143465)))";
    private static final String area13241 = "MULTIPOLYGON (((120.69573564330616 31.327309999189573, 120.69612595247793 31.327308370013657, 120.69718722428661 31.325717255890233, 120.69808270409025 31.325368971486622, 120.70169963540822 31.325569380113155, 120.70516017403206 31.325597389289385, 120.70689764247813 31.324660350571925, 120.75542191932003 31.329295587119677, 120.75550805594263 31.328123043584245, 120.72823987922153 31.325796147944697, 120.70711040566125 31.323744401744424, 120.70573841638927 31.324149119597184, 120.70488267408774 31.32470010686361, 120.69776330474302 31.32476569478398, 120.69647748854426 31.325444487565747, 120.69566285003579 31.326380456642895, 120.69573564330616 31.327309999189573)))";
    public static WKTReader commonWktReader = new WKTReader(JTSFactoryFinder.getGeometryFactory());

    @Test
    public void unionTest() throws ParseException {
        Geometry geometry = commonWktReader.read(area3332);
        Geometry geometry2 = commonWktReader.read(area13241);
        geometry.union(geometry2);
    }
}

then I get the exception like this:

org.locationtech.jts.geom.TopologyException: found non-noded intersection between LINESTRING ( 120.65259965054962 31.285279777143465, 120.67974663557145 31.288251142146198 ) and LINESTRING ( 120.65273638403228 31.28526053475525, 120.6525866745773 31.28529679390149 ) [ (120.65263910444314, 31.2852840955574, NaN) ]

my jts-core version is 1.17.0

@dr-jts
Copy link
Contributor

dr-jts commented Dec 24, 2020

That's because the first geometry is invalid (it has a self-intersection).
image

@hxhowl
Copy link
Author

hxhowl commented Dec 25, 2020

That's because the first geometry is invalid (it has a self-intersection).
image

@dr-jts Thank you for your answer。Is have any function to make the self-intersection polygon to a multipolygon or separate to same polygons without self-intersection?

@dr-jts
Copy link
Contributor

dr-jts commented Dec 25, 2020

Is have any function to make the self-intersection polygon to a multipolygon or separate to same polygons without self-intersection?

There's nothing in JTS right now that will preserve both parts of the polygon. But a standard trick is to run buffer(0) on the invalid polygon to fix it. (Note that there was a small bug affecting the buffer(0) functionality in some rare cases, which has been fixed in JTS 1.18)

@hxhowl
Copy link
Author

hxhowl commented Dec 25, 2020

Is have any function to make the self-intersection polygon to a multipolygon or separate to same polygons without self-intersection?

There's nothing in JTS right now that will preserve both parts of the polygon. But a standard trick is to run buffer(0) on the invalid polygon to fix it. (Note that there was a small bug affecting the buffer(0) functionality in some rare cases, which has been fixed in JTS 1.18)

@dr-jts when the JTS 1.18 can be used?

@dr-jts
Copy link
Contributor

dr-jts commented Dec 25, 2020

@hxhowl
Copy link
Author

hxhowl commented Dec 28, 2020

It's available now: https://github.com/locationtech/jts/releases/tag/jts-1.18.0

@dr-jts I had try the buffer(0) and Densifier.setValidated method。The data become a polygon that lost a part of segment。It can not make the data separate to same polygons that make the data complete and without self-intersection.

Today I Found a new solution Is there a way to convert a self intersecting polygon to a multipolygon in JTS?
that seems can solute my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants