buildGeometry()

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

buildGeometry()

Tore Halset
Hello.

I am using GeometryFactory.buildGeometry() to create a common geometry  
from a List of geometries. I just discovered that with a List with a  
single MultiPolygon the buildGeometry() returns a GeometryCollection  
of that single MultiPolygon. This is with jts-1.9.

 From the documentation, buildGeometry() will collect several Polygons  
to a common MultiPolygon. Perhaps it should also collect several  
MultiPolygons to a common MultiPolygon?

     public void testBuildGeometryWithSingleMultipolygon() {
                String a = "MULTIPOLYGON (((105 -9, 104.7719981 -9, 99 -9, 99  
-6.6735324, 99 -6.4769693, 99 -5.5, 99.1535114 -5.5, 99.6949253 -5.5,  
100.4429739 -5.5, 100.4968864 -5.5, 100.5130253 -5.5, 100.5618621  
-5.5, 100.5837587 -5.5, 100.6700839 -5.5, 101.1448967 -5.5,  
101.2765547 -5.5, 101.5917338 -5.5, 101.8689332 -5.5, 101.9972856  
-5.5, 102.0943862 -5.5, 102.2292855 -5.5, 102.2646017 -5.5,  
102.2882864 -5.5, 102.3513297 -5.5, 102.3738666 -5.5, 102.4045993  
-5.5, 102.4964335 -5.5, 102.701965 -5.5, 103.0498502 -5.5, 103.5239897  
-5.5, 103.965577 -5.5, 104.0948186 -5.5, 104.1457929 -5.5, 104.1895725  
-5.5, 105 -5.5, 105 -5.7329791, 105 -5.8032427, 105 -6.0876523, 105  
-6.1837199, 105 -6.23532, 105 -6.344359, 105 -6.434243, 105  
-6.6469016, 105 -7.1982306, 105 -7.6548277, 105 -8.0483834, 105  
-8.1408521, 105 -8.2582398, 105 -8.8460797, 105 -9)), ((100.547619 0,  
103.5872531 0, 103.5933789 0, 103.770947 0, 104.4792819 0, 104.5856461  
0, 105 0, 105 -2.3273102, 105 -2.4860457, 105 -5.5, 100.547619 0)))";
                GeometryFactory gf = new GeometryFactory();
                try {
                        Geometry g1 = new WKTReader().read(a);
                        assertTrue(g1.isValid());
                        Geometry g2 = gf.buildGeometry(Collections.singletonList(g1));
                        assertTrue(g2.isValid());
                        assertTrue("should be " + MultiPolygon.class + ", not " +  
g2.getClass(), g2 instanceof MultiPolygon);
                } catch (ParseException e) {
                        fail(e.getMessage());
                }
        }

Regards,
  - Tore.
_______________________________________________
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: buildGeometry()

Martin Davis
Tore,

I think you're right on this.  That's a design flaw in the
implementation (not really a bug, since it was intentional).

One reason this was done is that I felt that it was dangerous to combine
MultiPolygons, since it would be possible to create invalid geometry
this way (if the MPs overlapped).  However, the same situation exists
for Polygons, so this shouldn't really be a consideration.  In general
buildGeometry is really only useful in situations where it is known that
the geometries are safe to combine.  (However, this is actually fairly
common during algorithm implementation).

I would go further and suggest that given a heterogeneous set of geoms
(say { P, P, L, L, A, A } it should return a geometry with as much
combining as possible - so in this case, GC(MP, ML, MA).

I'll try and get a fix in for this soon.

Martin

Tore Halset wrote:

> Hello.
>
> I am using GeometryFactory.buildGeometry() to create a common geometry
> from a List of geometries. I just discovered that with a List with a
> single MultiPolygon the buildGeometry() returns a GeometryCollection
> of that single MultiPolygon. This is with jts-1.9.
>
> From the documentation, buildGeometry() will collect several Polygons
> to a common MultiPolygon. Perhaps it should also collect several
> MultiPolygons to a common MultiPolygon?
>
>     public void testBuildGeometryWithSingleMultipolygon() {
>         String a = "MULTIPOLYGON (((105 -9, 104.7719981 -9, 99 -9, 99
> -6.6735324, 99 -6.4769693, 99 -5.5, 99.1535114 -5.5, 99.6949253 -5.5,
> 100.4429739 -5.5, 100.4968864 -5.5, 100.5130253 -5.5, 100.5618621
> -5.5, 100.5837587 -5.5, 100.6700839 -5.5, 101.1448967 -5.5,
> 101.2765547 -5.5, 101.5917338 -5.5, 101.8689332 -5.5, 101.9972856
> -5.5, 102.0943862 -5.5, 102.2292855 -5.5, 102.2646017 -5.5,
> 102.2882864 -5.5, 102.3513297 -5.5, 102.3738666 -5.5, 102.4045993
> -5.5, 102.4964335 -5.5, 102.701965 -5.5, 103.0498502 -5.5, 103.5239897
> -5.5, 103.965577 -5.5, 104.0948186 -5.5, 104.1457929 -5.5, 104.1895725
> -5.5, 105 -5.5, 105 -5.7329791, 105 -5.8032427, 105 -6.0876523, 105
> -6.1837199, 105 -6.23532, 105 -6.344359, 105 -6.434243, 105
> -6.6469016, 105 -7.1982306, 105 -7.6548277, 105 -8.0483834, 105
> -8.1408521, 105 -8.2582398, 105 -8.8460797, 105 -9)), ((100.547619 0,
> 103.5872531 0, 103.5933789 0, 103.770947 0, 104.4792819 0, 104.5856461
> 0, 105 0, 105 -2.3273102, 105 -2.4860457, 105 -5.5, 100.547619 0)))";
>         GeometryFactory gf = new GeometryFactory();
>         try {
>             Geometry g1 = new WKTReader().read(a);
>             assertTrue(g1.isValid());
>             Geometry g2 =
> gf.buildGeometry(Collections.singletonList(g1));
>             assertTrue(g2.isValid());
>             assertTrue("should be " + MultiPolygon.class + ", not " +
> g2.getClass(), g2 instanceof MultiPolygon);
>         } catch (ParseException e) {
>             fail(e.getMessage());
>         }
>     }
>
> Regards,
>  - Tore.
> _______________________________________________
> 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
Loading...