Beispiel #1
0
/* 
 * Creates a 2D (xy) linestring from a list of 2D points.
 *
 * Parameter first is a gaiaPointPtr to the first point in a linked list of points which define the linestring.
 * All of the points in the list must be 2D (xy) points. There must be at least 2 points in the list.
 *
 * Returns a pointer to linestring containing all of the points in the list.
 */
static gaiaLinestringPtr
geoJSON_linestring_xy (struct geoJson_data *p_data, gaiaPointPtr first)
{
    gaiaPointPtr p = first;
    gaiaPointPtr p_n;
    int points = 0;
    int i = 0;
    gaiaLinestringPtr linestring;

    while (p != NULL)
      {
	  p = p->Next;
	  points++;
      }

    linestring = gaiaAllocLinestring (points);
    geoJsonMapDynAlloc (p_data, GEOJSON_DYN_LINESTRING, linestring);

    p = first;
    while (p != NULL)
      {
	  gaiaSetPoint (linestring->Coords, i, p->X, p->Y);
	  p_n = p->Next;
	  geoJsonMapDynClean (p_data, p);
	  gaiaFreePoint (p);
	  p = p_n;
	  i++;
      }

    return linestring;
}
int main (int argc, char *argv[])
{
    int result;
    int returnValue = 0;
    gaiaRingPtr interior;
    
    /* Common setup */
    gaiaLinestringPtr linestr1 = gaiaAllocLinestring(0);
    gaiaLinestringPtr linestr2 = gaiaAllocLinestring(0);
    gaiaPolygonPtr poly1 = gaiaAllocPolygon(0, 0);
    gaiaPolygonPtr poly2 = gaiaAllocPolygon(0, 0);
    
    /* Tests start here */
    
    /* zero length linestring */
    result = gaiaLinestringEquals(linestr1, linestr2);
    if (result != 1) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -1;
	goto exit;
    }
    
    /* non-matching linestring lengths */
    gaiaFreeLinestring (linestr1);
    gaiaFreeLinestring (linestr2);
    linestr1 = gaiaAllocLinestring(2);
    linestr2 = gaiaAllocLinestring(3);
    gaiaSetPoint(linestr1->Coords, 0, 1, 3); /* line1, first point */
    gaiaSetPoint(linestr1->Coords, 1, 2, 4); /* line1, second point */
    gaiaSetPoint(linestr2->Coords, 0, 4, -2); /* line2, first point */
    gaiaSetPoint(linestr2->Coords, 1, 1, 5); /* line2, second point */
    gaiaSetPoint(linestr2->Coords, 2, 3, 4); /* line2, third point */
    result = gaiaLinestringEquals(linestr1, linestr2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -2;
	goto exit;
    }
    
    /* identical lines */
    gaiaFreeLinestring (linestr2);
    linestr2 = gaiaCloneLinestring(linestr1);
    result = gaiaLinestringEquals(linestr1, linestr2);
    if (result != 1) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -3;
	goto exit;
    }
    
    /* not quite identical lines */
    gaiaSetPoint(linestr2->Coords, 1, 2, -4);
    result = gaiaLinestringEquals(linestr1, linestr2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -4;
	goto exit;
    }    
    
    /* zero length polygon */
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 1) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -5;
	goto exit;
    }
    
    /* matching polygons */
    gaiaFreePolygon (poly1);
    gaiaFreePolygon (poly2);
    poly1 = gaiaAllocPolygon(5, 0);
    gaiaSetPoint(poly1->Exterior->Coords, 0, 0, 0);
    gaiaSetPoint(poly1->Exterior->Coords, 1, 10, 0);
    gaiaSetPoint(poly1->Exterior->Coords, 2, 10, 10);
    gaiaSetPoint(poly1->Exterior->Coords, 3, 0, 10);
    gaiaSetPoint(poly1->Exterior->Coords, 4, 0, 0);
    poly2 = gaiaClonePolygon(poly1);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 1) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -6;
	goto exit;
    }
    
    /* not quite matching polygons */
    gaiaSetPoint(poly2->Exterior->Coords, 2, 10, -10);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -7;
	goto exit;
    }
    
    /* polygons with different numbers of interiors */
    gaiaFreePolygon (poly2);
    poly2 = gaiaAllocPolygon(5, 1);
    gaiaSetPoint(poly2->Exterior->Coords, 0, 0, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 1, 10, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 2, 10, 10);
    gaiaSetPoint(poly2->Exterior->Coords, 3, 0, 10);
    gaiaSetPoint(poly2->Exterior->Coords, 4, 0, 0);
    interior = gaiaAddInteriorRing(poly2, 0, 4);
    gaiaSetPoint(interior->Coords, 0, 1, 1);
    gaiaSetPoint(interior->Coords, 1, 3, 2);
    gaiaSetPoint(interior->Coords, 2, 3, 1);
    gaiaSetPoint(interior->Coords, 3, 1, 1);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -8;
	goto exit;
    }
    
    /* same exteriors and interiors */
    gaiaFreePolygon (poly1);
    poly1 = gaiaClonePolygon(poly2);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 1) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -9;
	goto exit;
    }
    
    /* slightly different interiors */
    gaiaSetPoint(interior->Coords, 2, 3, 3);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -10;
	goto exit;
    }

    /* different number of exterior points */
    gaiaFreePolygon(poly2);
    poly2 = gaiaAllocPolygon(4, 1);
    gaiaSetPoint(poly2->Exterior->Coords, 0, 0, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 1, 10, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 2, 10, 10);
    gaiaSetPoint(poly2->Exterior->Coords, 3, 0, 0);
    interior = gaiaAddInteriorRing(poly2, 0, 4);
    gaiaSetPoint(interior->Coords, 0, 1, 1);
    gaiaSetPoint(interior->Coords, 1, 3, 2);
    gaiaSetPoint(interior->Coords, 2, 3, 1);
    gaiaSetPoint(interior->Coords, 3, 1, 1);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -11;
	goto exit;
    }
    
    /* same exterior points, but different number of points on first interior */
    gaiaFreePolygon (poly2);
    poly2 = gaiaAllocPolygon(5, 1);
    gaiaSetPoint(poly2->Exterior->Coords, 0, 0, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 1, 10, 0);
    gaiaSetPoint(poly2->Exterior->Coords, 2, 10, 10);
    gaiaSetPoint(poly2->Exterior->Coords, 3, 0, 10);
    gaiaSetPoint(poly2->Exterior->Coords, 4, 0, 0);
    interior = gaiaAddInteriorRing(poly2, 0, 5);
    gaiaSetPoint(interior->Coords, 0, 1, 1);
    gaiaSetPoint(interior->Coords, 1, 3, 2);
    gaiaSetPoint(interior->Coords, 2, 3, 3);
    gaiaSetPoint(interior->Coords, 3, 1, 3);
    gaiaSetPoint(interior->Coords, 4, 1, 1);
    result = gaiaPolygonEquals(poly1, poly2);
    if (result != 0) {
	fprintf(stderr, "bad result at %s:%i: %i\n", __FILE__, __LINE__, result);
	returnValue = -12;
	goto exit;
    }
    
    /* Cleanup and exit */
exit:
    gaiaFreeLinestring (linestr1);
    gaiaFreeLinestring (linestr2);
    gaiaFreePolygon (poly1);
    gaiaFreePolygon (poly2);
    
    return returnValue;
}