GEOSIntersection returns the input

5 messages Options
Embed this post
Permalink
Giacomo Piva

GEOSIntersection returns the input

Reply Threaded More More options
Print post
Permalink
Hi all,
I'm writing because I have a problem with the GEOSIntersection function.
I'm using this function as follows:

GEOSCoordSeq coordseq = NULL, coordseq_I = NULL;
GEOSGeom area_1 = NULL, area_2 = NULL, intersection = NULL;

coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(5, 2);   //5 coords to
close the ring

GEOSCoordSeq_setX(coordseq, 0, ...);
GEOSCoordSeq_setY(coordseq, 0, ...);
[...]   //Fill the Seq with the coords value for the first ring

area_1 = GEOSGeom_createLinearRing(coordseq);

//Re-fill the coords for the second ring ...
GEOSCoordSeq_setX(coordseq, 0, ...);
GEOSCoordSeq_setY(coordseq, 0, ...);
[...]

area_2 = GEOSGeom_createLinearRing(coordseq);

intersection = GEOSIntersection(area_1, area_2);
//checks on valid and non empty geometry returns no error...

Now I get the coordinates of the intersection in this way:

GEOSGeom geom;
num = GEOSGetNumGeometries(intersection);   //num is 4, because there is
4 lines

for(i=0; i < num; i++) {
      geom = (GEOSGeom) GEOSGetGeometryN(intersection, i);   //Here I
get i-th line...

      coordseq_I = (GEOSCoordSeq) GEOSCoordSeq_create(2, 2);   //each
line has 2 point of 2 dimensions
      coordseq_I = (GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);

      GEOSCoordSeq_getX(coordseq_I, 0, alpha);    //the first point is
in 0-th position
      GEOSCoordSeq_getY(coordseq_I, 0, beta);
      printf("[%d] %.2lf %.2lf\n",i,*beta, *alpha);

      GEOSCoordSeq_getX(coordseq_I, 1, alpha);    //the second in the first
      GEOSCoordSeq_getY(coordseq_I, 1, beta);
      printf("[%d] %.2lf %.2lf\n\n",i,*beta, *alpha);
}



Now, if I print the coordinates of each point of the 4 geometries
(lines) composing the intersection ring, I get the same value of the
"area_2" ring.

Second Area:
[A] 43.22 -125.52
[B] 43.22 -106.47
[C] 22.71 -106.47
[D] 22.71 -125.52

Geometries: 4
[A] 43.22 -125.52
[B] 43.22 -106.47

[B] 43.22 -106.47
[C] 22.71 -106.47

[C] 22.71 -106.47
[D] 22.71 -125.52

[D] 22.71 -125.52
[A] 43.22 -125.52

Really I can't figure out what is wrong.
Someone can help me?

Thanks.

--
Giacomo Piva



_______________________________________________
geos-devel mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/geos-devel
David Turner-3

Re: GEOSIntersection returns the input

Reply Threaded More More options
Print post
Permalink
On Mon, 2009-08-24 at 13:56 +0200, Giacomo Piva wrote:
> Hi all,
> I'm writing because I have a problem with the GEOSIntersection function.
> I'm using this function as follows:

1. You should always provide compilable code, if at all possible, when
asking for help.

2. I think GEOSGeom_createLinearRing does not copy the input argument.



_______________________________________________
geos-devel mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/geos-devel
Giacomo Piva

Re: GEOSIntersection returns the input

Reply Threaded More More options
Print post
Permalink

>> Hi all,
>> I'm writing because I have a problem with the GEOSIntersection function.
>> I'm using this function as follows:
>>    
>
> 1. You should always provide compilable code, if at all possible, when
> asking for help.
>  
Follows the code I'm using.

    // Init GEOS
    GEOSMessageHandler error_function, notice_function;
    initGEOS(notice_function, error_function);

    GEOSCoordSeq coordseq = NULL;
    GEOSGeom area_1 = NULL, area_2 = NULL, intersection = NULL;

    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(5, 2);   //5 points
bi-dimensional

    GEOSCoordSeq_setX(coordseq, 0, inputdatat1->Lon_UL);    //upper left
    GEOSCoordSeq_setY(coordseq, 0, inputdatat1->Lat_UL);
    GEOSCoordSeq_setX(coordseq, 1, inputdatat1->Lon_LR);    //upper right
    GEOSCoordSeq_setY(coordseq, 1, inputdatat1->Lat_UL);
    GEOSCoordSeq_setX(coordseq, 2, inputdatat1->Lon_LR);    //lower right
    GEOSCoordSeq_setY(coordseq, 2, inputdatat1->Lat_LR);
    GEOSCoordSeq_setX(coordseq, 3, inputdatat1->Lon_UL);    //lower left
    GEOSCoordSeq_setY(coordseq, 3, inputdatat1->Lat_LR);
    GEOSCoordSeq_setX(coordseq, 4, inputdatat1->Lon_UL);    //upper left
    GEOSCoordSeq_setY(coordseq, 4, inputdatat1->Lat_UL);

    area_1 = GEOSGeom_createLinearRing(coordseq);  

    if((GEOSisEmpty(area_1) != 0) || (GEOSisValid(area_1) != 1)) {
        printf("No valid intersection found.\n");
        exit(2);    //invalid input parameter
    }

    GEOSCoordSeq_setX(coordseq, 0, inputdatat2->Lon_UL);    //upper left
    GEOSCoordSeq_setY(coordseq, 0, inputdatat2->Lat_UL);
    GEOSCoordSeq_setX(coordseq, 1, inputdatat2->Lon_LR);    //upper right
    GEOSCoordSeq_setY(coordseq, 1, inputdatat2->Lat_UL);
    GEOSCoordSeq_setX(coordseq, 2, inputdatat2->Lon_LR);    //lower right
    GEOSCoordSeq_setY(coordseq, 2, inputdatat2->Lat_LR);
    GEOSCoordSeq_setX(coordseq, 3, inputdatat2->Lon_UL);    //lower left
    GEOSCoordSeq_setY(coordseq, 3, inputdatat2->Lat_LR);
    GEOSCoordSeq_setX(coordseq, 4, inputdatat2->Lon_UL);    //upper left
    GEOSCoordSeq_setY(coordseq, 4, inputdatat2->Lat_UL);

    area_2 = GEOSGeom_createLinearRing(coordseq);

    if((GEOSisEmpty(area_2) != 0) || (GEOSisValid(area_2) != 1)) {
        printf("No valid intersection found.\n");
        exit(2);    //invalid input parameter
    }


    intersection = GEOSIntersection(area_1, area_2);

    if((GEOSisEmpty(intersection) != 0) || (GEOSisValid(intersection) !=
1)) {
        printf("No valid intersection found.\n");
        exit(2);    //invalid input parameter
    }

    //Getting coords for the vertex
    unsigned int num;      
    double xPoints[4];
    double yPoints[4];

    double *int_Lon_UL = (double*)malloc(sizeof(double)*1);
    double *int_Lat_UL = (double*)malloc(sizeof(double)*1);
    double *int_Lon_LR = (double*)malloc(sizeof(double)*1);
    double *int_Lat_LR = (double*)malloc(sizeof(double)*1);

    GEOSGeom geom;

    num = GEOSGetNumGeometries(intersection);
    printf("Geometries: %d\n",num);

    GEOSCoordSeq_destroy(coordseq);
    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(2, 2);   //2 points
bi-dimensional

    for(i=0; i < num; i++) {
        geom = (GEOSGeom) GEOSGetGeometryN(intersection, i);  

        coordseq = (GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);

        GEOSCoordSeq_getX(coordseq, 0, &xPoints[i]);
        GEOSCoordSeq_getY(coordseq, 0, &yPoints[i]);
    }

    //Filling the values in the right place
    *int_Lon_UL = xPoints[0];
    *int_Lat_UL = yPoints[0];
    *int_Lon_LR = xPoints[2];
    *int_Lat_LR = yPoints[2];

    // Finalizzo GEOS
    finishGEOS();

    printf("First Area:\t%.2lf %.2lf,%.2lf %.2lf,%.2lf %.2lf,%.2lf
%.2lf\n",inputdatat1->Lat_UL, inputdatat1->Lon_UL, inputdatat1->Lat_UL,
inputdatat1->Lon_LR, inputdatat1->Lat_LR, inputdatat1->Lon_LR,
inputdatat1->Lat_LR, inputdatat1->Lon_UL);
    printf("Second Area:\t%.2lf %.2lf,%.2lf %.2lf,%.2lf %.2lf,%.2lf
%.2lf\n",inputdatat2->Lat_UL, inputdatat2->Lon_UL, inputdatat2->Lat_UL,
inputdatat2->Lon_LR, inputdatat2->Lat_LR, inputdatat2->Lon_LR,
inputdatat2->Lat_LR, inputdatat2->Lon_UL);
    printf("intersection:\t%.2lf %.2lf,%.2lf %.2lf,%.2lf %.2lf,%.2lf
%.2lf\n",*int_Lat_UL, *int_Lon_UL, *int_Lat_UL, *int_Lon_LR,
*int_Lat_LR, *int_Lon_LR, *int_Lat_LR, *int_Lon_UL);



And the output is:
First Area:    42.46 -131.80,42.46 -112.91,21.96 -112.91,21.96 -131.80
Second Area:   43.22 -125.52,43.22 -106.47,22.71 -106.47,22.71 -125.52
intersection:  43.22 -125.52,43.22 -106.47,22.71 -106.47,22.71 -125.52

As you can see the intersection's vertex are the same of the second area.
> 2. I think GEOSGeom_createLinearRing does not copy the input argument
--
Giacomo


_______________________________________________
geos-devel mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/geos-devel
Jürgen E. Fischer

Re: GEOSIntersection returns the input

Reply Threaded More More options
Print post
Permalink
Hi Giacomo,

some comments:

On Tue, 25. Aug 2009 at 09:03:47 +0200, Giacomo Piva wrote:
>    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(5, 2);   //5 points bi-dimensional
[...]
>    area_1 = GEOSGeom_createLinearRing(coordseq);  
[...]
>    area_2 = GEOSGeom_createLinearRing(coordseq);

You need a new coordinate sequence here.  GEOSGeom_createLinearRing takes
ownership of it.

>    intersection = GEOSIntersection(area_1, area_2);

You're intersecting two rings here.   You might want to GEOSGeom_createPolygon
from the rings and intersect those.

>    GEOSCoordSeq_destroy(coordseq);

The coordinate sequence is owner by the linear ring.  So you shouldn't destroy
it here.

>    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(2, 2);   //2 points bi-dimensional

The (pointer to the) coordinate sequence you allocate here is lost...

>        coordseq = (GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);

...when you overwrite it here.

hth
Jürgen

--
Jürgen E. Fischer         norBIT GmbH               Tel. +49-4931-918175-20
Dipl.-Inf. (FH)           Rheinstraße 13            Fax. +49-4931-918175-50
Software Engineer         D-26506 Norden               http://www.norbit.de

--
norBIT Gesellschaft fuer Unternehmensberatung und Informationssysteme mbH
Rheinstrasse 13, 26506 Norden
GF: Jelto Buurman, HR: Amtsgericht Emden, HRB 5502

_______________________________________________
geos-devel mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/geos-devel
Giacomo Piva

Re: GEOSIntersection returns the input

Reply Threaded More More options
Print post
Permalink
Thank you.
Finally I get the intersection by using 2 polygons from linear ring


Jürgen E. Fischer ha scritto:

> Hi Giacomo,
>
> some comments:
>
> On Tue, 25. Aug 2009 at 09:03:47 +0200, Giacomo Piva wrote:
>  
>>    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(5, 2);   //5 points bi-dimensional
>>    
> [...]
>  
>>    area_1 = GEOSGeom_createLinearRing(coordseq);  
>>    
> [...]
>  
>>    area_2 = GEOSGeom_createLinearRing(coordseq);
>>    
>
> You need a new coordinate sequence here.  GEOSGeom_createLinearRing takes
> ownership of it.
>
>  
>>    intersection = GEOSIntersection(area_1, area_2);
>>    
>
> You're intersecting two rings here.   You might want to GEOSGeom_createPolygon
> from the rings and intersect those.
>
>  
>>    GEOSCoordSeq_destroy(coordseq);
>>    
>
> The coordinate sequence is owner by the linear ring.  So you shouldn't destroy
> it here.
>
>  
>>    coordseq = (GEOSCoordSeq) GEOSCoordSeq_create(2, 2);   //2 points bi-dimensional
>>    
>
> The (pointer to the) coordinate sequence you allocate here is lost...
>
>  
>>        coordseq = (GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
>>    
>
> ...when you overwrite it here.
>
> hth
> Jürgen
>
>  


--
Giacomo

_______________________________________________
geos-devel mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/geos-devel