Quantcast

Polygon union with a hole

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Polygon union with a hole

Doug Smith-3

Hello,

 

I'm using JTS to generate polygons that are the combination of multiple postal code polygons. I put all of the postal code polygons into a GeometryCollection, then iterate over them and call union on each one to build the combined polygon. Here's the code I'm currently using:

 

Geometry unionPolygon = null;

GeometryCollectionIterator iter = new GeometryCollectionIterator(geomColl);

// Skip first, because iterator returns the whole collection as first element (!)

if (iter.hasNext()) iter.next();

// Iterate through each polygon and union together

while (iter.hasNext()) {

      Geometry currGeom = (Geometry)iter.next();

      if (unionPolygon == null)

            unionPolygon = currGeom;

      else {

            unionPolygon = unionPolygon.union(currGeom);

      }

}

 

The problem is that this technique does not support holes in the middle of the combined polygon. I have posted three images as an example.

 

This link points to an image that shows the "before" state of the polygon -- before the postal code is added that closes the hole in the middle:

http://drop.io/mapster/asset/terr-union1-jpg

 

This link shows the postal code that will be unioned to the combined polygon:

http://drop.io/mapster/asset/terr-union2-jpg

 

And this link shows the result of unioning the new postal code to the combined polygon:

http://drop.io/mapster/asset/terr-union3-jpg

 

I'm wondering if there is a way to detect whether the polygon I'm about to union will close the hole, and then do something else to actually cut the hole instead. Any other thoughts would be very welcome. Thank you for your time!

Doug

---

Doug Smith

ELP Web Developer

http://www.daveramsey.com

 

"Life is an occasion, rise to it" -- Mr. Magorium

 


_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Polygon union with a hole

Martin Davis
Doug,

What version of JTS are you using?

Your logic looks right, but that result doesn't seem correct.  The
union() method should preserve any holes that occur during the union.

I agree that the semantics of GeometryCollectionIterator are a bit odd.  
There was some reason way back when for returning the object itself as
well as the components, but it escapes me now!

Are there any other polygons in the collection which would cover the
area where the hole is?  Are they getting included for some reason?

You could try using GeometryExtracter to get a list of all the Polygons,
and then union that.  You can also use Unary Union (Geometry.union())
directly on the GeometryCollection - that should work as well.

Failing any of this, post the dataset and some test code, and I can look
into the problem.

Doug Smith wrote:

>
> Hello,
>
>  
>
> I'm using JTS to generate polygons that are the combination of
> multiple postal code polygons. I put all of the postal code polygons
> into a GeometryCollection, then iterate over them and call union on
> each one to build the combined polygon. Here's the code I'm currently
> using:
>
>  
>
> Geometry unionPolygon = *null*;
>
> GeometryCollectionIterator iter = *new*
> GeometryCollectionIterator(geomColl);
>
> // Skip first, because iterator returns the whole collection as first
> element (!)
>
> *if* (iter.hasNext()) iter.next();
>
> // Iterate through each polygon and union together
>
> *while* (iter.hasNext()) {
>
>       Geometry currGeom = (Geometry)iter.next();
>
>       *if* (unionPolygon == *null*)
>
>             unionPolygon = currGeom;
>
>       *else* {
>
>             unionPolygon = unionPolygon.union(currGeom);
>
>       }
>
> }
>
>  
>
> The problem is that this technique does not support holes in the
> middle of the combined polygon. I have posted three images as an example.
>
>  
>
> This link points to an image that shows the "before" state of the
> polygon -- before the postal code is added that closes the hole in the
> middle:
>
> http://drop.io/mapster/asset/terr-union1-jpg
>
>  
>
> This link shows the postal code that will be unioned to the combined
> polygon:
>
> http://drop.io/mapster/asset/terr-union2-jpg
>
>  
>
> And this link shows the result of unioning the new postal code to the
> combined polygon:
>
> http://drop.io/mapster/asset/terr-union3-jpg
>
>  
>
> I'm wondering if there is a way to detect whether the polygon I'm
> about to union will close the hole, and then do something else to
> actually cut the hole instead. Any other thoughts would be very
> welcome. Thank you for your time!
>
> Doug
>
> ---
>
> Doug Smith
>
> ELP Web Developer
>
> http://www.daveramsey.com
>
>  
>
> "Life is an occasion, rise to it" -- Mr. Magorium
>
>  
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Polygon union with a hole

Doug Smith-3
Thank you for your fast response!  I am using JTS 1.10.

I tried using the other two techniques you suggested and got the same
results.

I uploaded all of the polygons that I'm trying to union with a hole to
my drop here:
http://drop.io/mapster/asset/polygon-wkt-zip

I also uploaded two versions of the entire thing that I'm trying to
create. This are the individual postal codes:
http://drop.io/mapster/asset/terr-postal-jpg

And this is the union of them generated by JTS:
http://drop.io/mapster/asset/terr-union-jpg

You can see that the hole near Albany, OR isn't shown in the union
version.

I appreciate your help very much.

Thanks,

Doug

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Martin
Davis
Sent: Thursday, January 22, 2009 10:50 AM
To: JTS Topology Suite Development
Subject: Re: [jts-devel] Polygon union with a hole

Doug,

What version of JTS are you using?

Your logic looks right, but that result doesn't seem correct.  The
union() method should preserve any holes that occur during the union.

I agree that the semantics of GeometryCollectionIterator are a bit odd.

There was some reason way back when for returning the object itself as
well as the components, but it escapes me now!

Are there any other polygons in the collection which would cover the
area where the hole is?  Are they getting included for some reason?

You could try using GeometryExtracter to get a list of all the Polygons,

and then union that.  You can also use Unary Union (Geometry.union())
directly on the GeometryCollection - that should work as well.

Failing any of this, post the dataset and some test code, and I can look

into the problem.

Doug Smith wrote:

>
> Hello,
>
>  
>
> I'm using JTS to generate polygons that are the combination of
> multiple postal code polygons. I put all of the postal code polygons
> into a GeometryCollection, then iterate over them and call union on
> each one to build the combined polygon. Here's the code I'm currently
> using:
>
>  
>
> Geometry unionPolygon = *null*;
>
> GeometryCollectionIterator iter = *new*
> GeometryCollectionIterator(geomColl);
>
> // Skip first, because iterator returns the whole collection as first
> element (!)
>
> *if* (iter.hasNext()) iter.next();
>
> // Iterate through each polygon and union together
>
> *while* (iter.hasNext()) {
>
>       Geometry currGeom = (Geometry)iter.next();
>
>       *if* (unionPolygon == *null*)
>
>             unionPolygon = currGeom;
>
>       *else* {
>
>             unionPolygon = unionPolygon.union(currGeom);
>
>       }
>
> }
>
>  
>
> The problem is that this technique does not support holes in the
> middle of the combined polygon. I have posted three images as an
example.
>
>  
>
> This link points to an image that shows the "before" state of the
> polygon -- before the postal code is added that closes the hole in the

> middle:
>
> http://drop.io/mapster/asset/terr-union1-jpg
>
>  
>
> This link shows the postal code that will be unioned to the combined
> polygon:
>
> http://drop.io/mapster/asset/terr-union2-jpg
>
>  
>
> And this link shows the result of unioning the new postal code to the
> combined polygon:
>
> http://drop.io/mapster/asset/terr-union3-jpg
>
>  
>
> I'm wondering if there is a way to detect whether the polygon I'm
> about to union will close the hole, and then do something else to
> actually cut the hole instead. Any other thoughts would be very
> welcome. Thank you for your time!
>
> Doug
>
> ---
>
> Doug Smith
>
> ELP Web Developer
>
> http://www.daveramsey.com
>
>  
>
> "Life is an occasion, rise to it" -- Mr. Magorium
>
>  
>
>
------------------------------------------------------------------------
>
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Polygon union with a hole

Martin Davis
Hmmm..   OpenJUMP unions these correctly - but I think it's using the
new Cascaded Union functionality.

Can you randomize the order of the polygons and see if the error
persists?  It *might* be some kind of strange round-off error
interaction - although JTS is pretty good at avoiding this now, so I
think (hope) that's unlikely.

Can you prepare a simple Java test class which exhibits the behaviour?  
Without knowing how you are providing the polygons, there's always a
chance that it's something external which is causing a problem

Doug Smith wrote:

> Thank you for your fast response!  I am using JTS 1.10.
>
> I tried using the other two techniques you suggested and got the same
> results.
>
> I uploaded all of the polygons that I'm trying to union with a hole to
> my drop here:
> http://drop.io/mapster/asset/polygon-wkt-zip
>
> I also uploaded two versions of the entire thing that I'm trying to
> create. This are the individual postal codes:
> http://drop.io/mapster/asset/terr-postal-jpg
>
> And this is the union of them generated by JTS:
> http://drop.io/mapster/asset/terr-union-jpg
>
> You can see that the hole near Albany, OR isn't shown in the union
> version.
>
> I appreciate your help very much.
>
> Thanks,
>
> Doug
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Martin
> Davis
> Sent: Thursday, January 22, 2009 10:50 AM
> To: JTS Topology Suite Development
> Subject: Re: [jts-devel] Polygon union with a hole
>
> Doug,
>
> What version of JTS are you using?
>
> Your logic looks right, but that result doesn't seem correct.  The
> union() method should preserve any holes that occur during the union.
>
> I agree that the semantics of GeometryCollectionIterator are a bit odd.
>
> There was some reason way back when for returning the object itself as
> well as the components, but it escapes me now!
>
> Are there any other polygons in the collection which would cover the
> area where the hole is?  Are they getting included for some reason?
>
> You could try using GeometryExtracter to get a list of all the Polygons,
>
> and then union that.  You can also use Unary Union (Geometry.union())
> directly on the GeometryCollection - that should work as well.
>
> Failing any of this, post the dataset and some test code, and I can look
>
> into the problem.
>
> Doug Smith wrote:
>  
>> Hello,
>>
>>  
>>
>> I'm using JTS to generate polygons that are the combination of
>> multiple postal code polygons. I put all of the postal code polygons
>> into a GeometryCollection, then iterate over them and call union on
>> each one to build the combined polygon. Here's the code I'm currently
>> using:
>>
>>  
>>
>> Geometry unionPolygon = *null*;
>>
>> GeometryCollectionIterator iter = *new*
>> GeometryCollectionIterator(geomColl);
>>
>> // Skip first, because iterator returns the whole collection as first
>> element (!)
>>
>> *if* (iter.hasNext()) iter.next();
>>
>> // Iterate through each polygon and union together
>>
>> *while* (iter.hasNext()) {
>>
>>       Geometry currGeom = (Geometry)iter.next();
>>
>>       *if* (unionPolygon == *null*)
>>
>>             unionPolygon = currGeom;
>>
>>       *else* {
>>
>>             unionPolygon = unionPolygon.union(currGeom);
>>
>>       }
>>
>> }
>>
>>  
>>
>> The problem is that this technique does not support holes in the
>> middle of the combined polygon. I have posted three images as an
>>    
> example.
>  
>>  
>>
>> This link points to an image that shows the "before" state of the
>> polygon -- before the postal code is added that closes the hole in the
>>    
>
>  
>> middle:
>>
>> http://drop.io/mapster/asset/terr-union1-jpg
>>
>>  
>>
>> This link shows the postal code that will be unioned to the combined
>> polygon:
>>
>> http://drop.io/mapster/asset/terr-union2-jpg
>>
>>  
>>
>> And this link shows the result of unioning the new postal code to the
>> combined polygon:
>>
>> http://drop.io/mapster/asset/terr-union3-jpg
>>
>>  
>>
>> I'm wondering if there is a way to detect whether the polygon I'm
>> about to union will close the hole, and then do something else to
>> actually cut the hole instead. Any other thoughts would be very
>> welcome. Thank you for your time!
>>
>> Doug
>>
>> ---
>>
>> Doug Smith
>>
>> ELP Web Developer
>>
>> http://www.daveramsey.com
>>
>>  
>>
>> "Life is an occasion, rise to it" -- Mr. Magorium
>>
>>  
>>
>>
>>    
> ------------------------------------------------------------------------
>  
>> _______________________________________________
>> jts-devel mailing list
>> [hidden email]
>> http://lists.refractions.net/mailman/listinfo/jts-devel
>>  
>>    
>
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Polygon union with a hole

Doug Smith-3
Martin, you are correct. JTS does union these polygons correctly. It's
the code I'm using to transform the resulting WKT into MapQuest
FeatureCollection objects for display in our Flex application that is
not including the hole.  

Here is the relevant portion of the simple little test class I created
to prove that unioning does work, just for other readers:

public static void main(String [] args)
{
        try {
                UnionTest ut = new UnionTest();
                List<Geometry> polygons =
ut.readWKTFromFile("polygon_wkt.txt");
                Geometry unionResult =
CascadedPolygonUnion.union(polygons);
                ut.writeWKTToFile(unionResult, " union_wkt.txt");
                System.out.println("DONE: " + unionResult.toText());
        }
        catch (Exception e) {
                System.out.println("EXCEPTION: " + e.getMessage());
                e.printStackTrace(System.out);
        }
}

Thank you again for your fast response, and for such a powerful set of
libraries. JTS is an extremely important tool in our GIS toolset.

Doug
---
Doug Smith
ELP Web Developer
http://www.daveramsey.com
 
"Life is an occasion, rise to it" -- Mr. Magorium

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Martin
Davis
Sent: Thursday, January 22, 2009 7:38 PM
To: JTS Topology Suite Development
Subject: Re: [jts-devel] Polygon union with a hole

Hmmm..   OpenJUMP unions these correctly - but I think it's using the
new Cascaded Union functionality.

Can you randomize the order of the polygons and see if the error
persists?  It *might* be some kind of strange round-off error
interaction - although JTS is pretty good at avoiding this now, so I
think (hope) that's unlikely.

Can you prepare a simple Java test class which exhibits the behaviour?  
Without knowing how you are providing the polygons, there's always a
chance that it's something external which is causing a problem

Doug Smith wrote:

> Thank you for your fast response!  I am using JTS 1.10.
>
> I tried using the other two techniques you suggested and got the same
> results.
>
> I uploaded all of the polygons that I'm trying to union with a hole to
> my drop here:
> http://drop.io/mapster/asset/polygon-wkt-zip
>
> I also uploaded two versions of the entire thing that I'm trying to
> create. This are the individual postal codes:
> http://drop.io/mapster/asset/terr-postal-jpg
>
> And this is the union of them generated by JTS:
> http://drop.io/mapster/asset/terr-union-jpg
>
> You can see that the hole near Albany, OR isn't shown in the union
> version.
>
> I appreciate your help very much.
>
> Thanks,
>
> Doug
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Martin
> Davis
> Sent: Thursday, January 22, 2009 10:50 AM
> To: JTS Topology Suite Development
> Subject: Re: [jts-devel] Polygon union with a hole
>
> Doug,
>
> What version of JTS are you using?
>
> Your logic looks right, but that result doesn't seem correct.  The
> union() method should preserve any holes that occur during the union.
>
> I agree that the semantics of GeometryCollectionIterator are a bit
odd.
>
> There was some reason way back when for returning the object itself as

> well as the components, but it escapes me now!
>
> Are there any other polygons in the collection which would cover the
> area where the hole is?  Are they getting included for some reason?
>
> You could try using GeometryExtracter to get a list of all the
Polygons,
>
> and then union that.  You can also use Unary Union (Geometry.union())
> directly on the GeometryCollection - that should work as well.
>
> Failing any of this, post the dataset and some test code, and I can
look

>
> into the problem.
>
> Doug Smith wrote:
>  
>> Hello,
>>
>>  
>>
>> I'm using JTS to generate polygons that are the combination of
>> multiple postal code polygons. I put all of the postal code polygons
>> into a GeometryCollection, then iterate over them and call union on
>> each one to build the combined polygon. Here's the code I'm currently

>> using:
>>
>>  
>>
>> Geometry unionPolygon = *null*;
>>
>> GeometryCollectionIterator iter = *new*
>> GeometryCollectionIterator(geomColl);
>>
>> // Skip first, because iterator returns the whole collection as first

>> element (!)
>>
>> *if* (iter.hasNext()) iter.next();
>>
>> // Iterate through each polygon and union together
>>
>> *while* (iter.hasNext()) {
>>
>>       Geometry currGeom = (Geometry)iter.next();
>>
>>       *if* (unionPolygon == *null*)
>>
>>             unionPolygon = currGeom;
>>
>>       *else* {
>>
>>             unionPolygon = unionPolygon.union(currGeom);
>>
>>       }
>>
>> }
>>
>>  
>>
>> The problem is that this technique does not support holes in the
>> middle of the combined polygon. I have posted three images as an
>>    
> example.
>  
>>  
>>
>> This link points to an image that shows the "before" state of the
>> polygon -- before the postal code is added that closes the hole in
the

>>    
>
>  
>> middle:
>>
>> http://drop.io/mapster/asset/terr-union1-jpg
>>
>>  
>>
>> This link shows the postal code that will be unioned to the combined
>> polygon:
>>
>> http://drop.io/mapster/asset/terr-union2-jpg
>>
>>  
>>
>> And this link shows the result of unioning the new postal code to the

>> combined polygon:
>>
>> http://drop.io/mapster/asset/terr-union3-jpg
>>
>>  
>>
>> I'm wondering if there is a way to detect whether the polygon I'm
>> about to union will close the hole, and then do something else to
>> actually cut the hole instead. Any other thoughts would be very
>> welcome. Thank you for your time!
>>
>> Doug
>>
>> ---
>>
>> Doug Smith
>>
>> ELP Web Developer
>>
>> http://www.daveramsey.com
>>
>>  
>>
>> "Life is an occasion, rise to it" -- Mr. Magorium
>>
>>  
>>
>>
>>    
>
------------------------------------------------------------------------
>  
>> _______________________________________________
>> jts-devel mailing list
>> [hidden email]
>> http://lists.refractions.net/mailman/listinfo/jts-devel
>>  
>>    
>
>  

--
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Loading...