From: Andy C. <gt...@gm...> - 2009-03-24 19:56:34
|
I eventually got it to work by directly creating various Impl objects rather than using factories. I know that's not the best way, but I can't figure out which factories to use without getting exceptions. Anyway, it's not giving me the result I would expect. What am I doing wrong here? --- PositionFactory posF = new PositionFactoryImpl(DefaultGeographicCRS.WGS84); PrimitiveFactory primF = new PrimitiveFactoryImpl(DefaultGeographicCRS.WGS84); Point[] ringPoints = new Point[] { new PointImpl(posF.createDirectPosition(new double[] {40, -100})), new PointImpl(posF.createDirectPosition(new double[] {40, -90})), new PointImpl(posF.createDirectPosition(new double[] {30, -90})), new PointImpl(posF.createDirectPosition(new double[] {30, -100})), new PointImpl(posF.createDirectPosition(new double[] {40, -100})) }; Ring r = primF.createRing(Arrays.asList(ringPoints)); Polygon p = new PolygonImpl(primF.createSurfaceBoundary(r, Collections.EMPTY_LIST)); DirectPosition pos = posF.createDirectPosition(new double[] {35, -95}); Point pt = new PointImpl(pos); System.out.println("Is "+pt+" in "+r+"? "+r.contains(pt)); --- The output is: Is org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl@4898424d in org.geotools.geometry.jts.spatialschema.geometry.primitive.RingImpl@19cc1b? false (35,-95) should easily be inside the polygon, so either I'm doing something wrong (my guess) or there's a bug somewhere. Thanks, Andy On Tue, Mar 24, 2009 at 11:42, Andy Cox <gt...@gm...> wrote: > Sorry I was executing the wrong class in Eclipse when I got that exception. > > However, I did try a full point-in-polygon example (my main goal here) > and got that exception. Here's my sample code: > > --- > PositionFactory posF = new PositionFactoryImpl(DefaultGeographicCRS.WGS84); > PrimitiveFactory primF = new PrimitiveFactoryImpl(DefaultGeographicCRS.WGS84); > GeometryFactory geomF = new JTSGeometryFactory(DefaultGeographicCRS.WGS84); > > PointArray points = posF.createPointArray(); > > DirectPosition[] ringPoints = new DirectPosition[] { > posF.createDirectPosition(new double[] {40, -100}), > posF.createDirectPosition(new double[] {40, -90}), > posF.createDirectPosition(new double[] {30, -90}), > posF.createDirectPosition(new double[] {30, -100}), > posF.createDirectPosition(new double[] {40, -100}), > }; > > Ring r = primF.createRing(Arrays.asList(ringPoints)); > > DirectPosition pt = posF.createDirectPosition(new double[] {35, -95}); > System.out.println("Is "+pt+" in "+r+"? "+r.contains(pt)); > --- > > Running this gives the same exception as in my previous example: > > Exception in thread "main" java.lang.ClassCastException: > org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl > at org.geotools.geometry.jts.spatialschema.geometry.complex.ComplexImpl.computeJTSPeer(ComplexImpl.java:150) > at org.geotools.geometry.jts.spatialschema.geometry.GeometryImpl.getJTSGeometry(GeometryImpl.java:152) > at org.geotools.geometry.jts.spatialschema.geometry.GeometryImpl.contains(GeometryImpl.java:506) > at my code... > > Am I doing something wrong, or is this a library problem? |