Newbie - Merge Polygons

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

Newbie - Merge Polygons

Lee Goddard-3-3
Sorry if this is the wrong place to ask.

I'm not quite sure where to start with JTS: I have a database with a a
text field holding large number of latitude/longitude pairs seperated by
spaces to represent polygons.

Rendering these as GIFs with awt has proven easy enough, as has
producing KML, but the size of my KML file, even gzipped, is too large
to publish.

Merging these shapes with awt.geometry has proven extremely inefficient.

Some kind soul pointed me to JTS, and I gather that the geometry.union()
method would be beneficial.

Would that seem to make sense? Are there any code samples available
using the classes involved?

Many thanks in anticipation
Lee
_______________________________________________
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: Newbie - Merge Polygons

mbedward
Hi Lee,

If you have only a small number number of polygons to merge then
sequentially unioning them would be ok. But if you've got lots of
them, UnaryUnionOp is your friend.

The code would look something like this..

    GeometryFactory gf = new GeometryFactory();
    List<Geometry> polys = new ArrayList<Geometry>();

    for ( each rec in your database ) {
        String text = ...
        String[] values = text.split("\\s");  // assuming whitespace delimited

        Coordinate[] coords = new Coordinate[values.length / 2];
        for (int i = 0, k = 0; i < values.length; i += 2, k++) {
            coords[k] = new Coordinate(
                    Double.parseDouble(values[i]),
                    Double.parseDouble(values[i+1]));
        }

        Geometry poly = gf.createPolygon(gf.createLinearRing(coords), null);
        polys.add(poly);
    }

    // now union the polygons together
    Geometry union = UnaryUnionOp.union(polys);

Hope this helps,

Michael
_______________________________________________
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: Newbie - Merge Polygons

mbedward
PS. I should have also mentioned that the code example assumes the
pairs are in lon-lat order. If they're in lat-lon order just switch
the indices in the loop.

Michael
_______________________________________________
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: Newbie - Merge Polygons

Lee Goddard-3-3
In reply to this post by mbedward
That looks really helpful, thank you very much!

Cheers
Lee

Michael Bedward wrote:

> Hi Lee,
>
> If you have only a small number number of polygons to merge then
> sequentially unioning them would be ok. But if you've got lots of
> them, UnaryUnionOp is your friend.
>
> The code would look something like this..
>
>     GeometryFactory gf = new GeometryFactory();
>     List<Geometry> polys = new ArrayList<Geometry>();
>
>     for ( each rec in your database ) {
>         String text = ...
>         String[] values = text.split("\\s");  // assuming whitespace delimited
>
>         Coordinate[] coords = new Coordinate[values.length / 2];
>         for (int i = 0, k = 0; i < values.length; i += 2, k++) {
>             coords[k] = new Coordinate(
>                     Double.parseDouble(values[i]),
>                     Double.parseDouble(values[i+1]));
>         }
>
>         Geometry poly = gf.createPolygon(gf.createLinearRing(coords), null);
>         polys.add(poly);
>     }
>
>     // now union the polygons together
>     Geometry union = UnaryUnionOp.union(polys);
>
> Hope this helps,

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