I changed the code to build the factories like this:

                GeometryBuilder builder = new 
GeometryBuilder(DefaultGeographicCRS.WGS84);
                PositionFactory posF = builder.getPositionFactory();
                PrimitiveFactory primF = builder.getPrimitiveFactory();
                GeometryFactory geomF = builder.getGeometryFactory();

(I haven't updated any JARs, but I can override the exception I get
about not being able to find JTSGeometryFactory by setting a system
property.)

However, I still get what I think is the wrong answer from the
contains() method. (The point 35,-95 should easily be in the box with
upper left 40, -100 and lower right 30,-90.)

If you run this code in your environment, do you get the same answer?
Am I incorrectly understanding the semantics of Ring.contains()? I've
run essentially the same test with the raw JTS library (1.10) and it
works as I expect, but I don't understand whether JTS takes into
account the curvature of the earth, or whether it assumes a flat
plane. Obviously, over larger distances, this will introduce error.

Here's the code again for reference:

import org.geotools.geometry.GeometryBuilder;
import org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl;
import org.geotools.referencing.crs.DefaultGeographicCRS;

import org.opengis.geometry.DirectPosition;
import org.opengis.geometry.PositionFactory;
import org.opengis.geometry.coordinate.GeometryFactory;
import org.opengis.geometry.coordinate.PointArray;
import org.opengis.geometry.coordinate.Polygon;
import org.opengis.geometry.primitive.Point;
import org.opengis.geometry.primitive.PrimitiveFactory;
import org.opengis.geometry.primitive.Ring;

public class PolygonTest {
        public static void main(String[] args) {

                
System.setProperty("org.opengis.geometry.coordinate.GeometryFactory",
"org.geotools.geometry.jts.spatialschema.geometry.geometry.JTSGeometryFactory");
                GeometryBuilder builder = new 
GeometryBuilder(DefaultGeographicCRS.WGS84);
                PositionFactory posF = builder.getPositionFactory();
                PrimitiveFactory primF = builder.getPrimitiveFactory();
                GeometryFactory geomF = builder.getGeometryFactory();

                PointArray points = posF.createPointArray();

                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));
                Polygon p = geomF.createPolygon(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));
        }
}

Thanks,

Andy

On Wed, Mar 25, 2009 at 08:36, Jody Garnett <jody.garn...@gmail.com> wrote:
> Did you ever try the GoemetryBuilder? It was suppoesd to help you set
> up the factories to all work against the same CRS.
>
> This page of instructions:
> - http://docs.codehaus.org/display/GEOTDOC/01+Building+Geometry+Objects
>
> Applies for both the gt-geomety or gt-jts-wraper plugins (or is
> supposed to if the manifest/services folder is now correct).
>
> Jody
>
> On Wed, Mar 25, 2009 at 6:31 AM, Andy Cox <gth...@gmail.com> wrote:
>> 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.pointi...@4898424d
>> in 
>> org.geotools.geometry.jts.spatialschema.geometry.primitive.ringi...@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 <gth...@gmail.com> 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?
>>
>

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to