Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTS/Geotools Correct union/difference of multiple geometry

Tags:

java

geotools

jts

Problem :

I have a shape file that contains the targeted area (yellow).

I have a shape file that contains the buildings (green).

I need the white space in the yellow area.

picture : ://db.tt/kjjXZlQF

My solutions :

  1. Get all buildings in that area

    Filter inPolygon = CQL.toFilter("WITHIN(the_geom,"+wktwriter.write(targetarea) + ")");

    FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures(inPolygon);//this works

  2. From every building get it's geometry and use difference on the target area

    toCover = toCover.Difference(building);

OR second solution :

Union of every building and then difference at the end.

OR third solution :

Put them all in a GeomtryCollection call union and then use difference

Everyone of these solutions give me something like the following picture

picture : https://dl.dropboxusercontent.com/u/639458/stackoverflow/stfr2.png

Let it be clear I tried out several ways to solve this problem, by using different ways of creating / making or using it. Even with the given code in the site below it did not work correct.

http://docs.geotools.org/latest/userguide/library/jts/combine.html

like image 212
HappyProgrammerUser Avatar asked Nov 13 '22 06:11

HappyProgrammerUser


1 Answers

My problems were solved in the end like @mdup had suggested the use of

.buffer(0)
like image 120
HappyProgrammerUser Avatar answered Nov 15 '22 12:11

HappyProgrammerUser