Example #1
0
/**
 * Takes a GEOMETRY and returns a GeoJson representation
 */
char *
lwgeom_to_geojson(const LWGEOM *geom, char *srs, int precision, int has_bbox)
{
	int type = geom->type;
	GBOX *bbox = NULL;
	GBOX tmp;

	if ( precision > OUT_MAX_DOUBLE_PRECISION ) precision = OUT_MAX_DOUBLE_PRECISION;

	if (has_bbox) 
	{
		/* Whether these are geography or geometry, 
		   the GeoJSON expects a cartesian bounding box */
		lwgeom_calculate_gbox_cartesian(geom, &tmp);
		bbox = &tmp;
	}		

	switch (type)
	{
	case POINTTYPE:
		return asgeojson_point((LWPOINT*)geom, srs, bbox, precision);
	case LINETYPE:
		return asgeojson_line((LWLINE*)geom, srs, bbox, precision);
	case POLYGONTYPE:
		return asgeojson_poly((LWPOLY*)geom, srs, bbox, precision);
	case MULTIPOINTTYPE:
		return asgeojson_multipoint((LWMPOINT*)geom, srs, bbox, precision);
	case MULTILINETYPE:
		return asgeojson_multiline((LWMLINE*)geom, srs, bbox, precision);
	case MULTIPOLYGONTYPE:
		return asgeojson_multipolygon((LWMPOLY*)geom, srs, bbox, precision);
	case COLLECTIONTYPE:
		return asgeojson_collection((LWCOLLECTION*)geom, srs, bbox, precision);
	default:
		lwerror("lwgeom_to_geojson: '%s' geometry type not supported",
		        lwtype_name(type));
	}

	/* Never get here */
	return NULL;
}
char*
geom_to_geojson(GEOSGeom geom, char *srs, int precision, int has_bbox)
{
    int type = GEOSGeomTypeId(geom)+1;
    bbox3D *bbox;

    if ( precision > OUT_MAX_DOUBLE_PRECISION ) precision = OUT_MAX_DOUBLE_PRECISION;

    if (has_bbox) 
    {
        bbox3DFromGeos(&bbox, geom);
    }		

    switch (type)
    {
        case wkbPoint_mdb:
            return asgeojson_point(geom, srs, bbox, precision);
        case wkbLineString_mdb:
            return asgeojson_line(geom, srs, bbox, precision);
        case wkbPolygon_mdb:
            return asgeojson_poly(geom, srs, bbox, precision);
        case wkbMultiPoint_mdb:
            return asgeojson_multipoint(geom, srs, bbox, precision);
        case wkbMultiLineString_mdb:
            return asgeojson_multiline(geom, srs, bbox, precision);
        case wkbMultiPolygon_mdb:
            return asgeojson_multipolygon(geom, srs, bbox, precision);
        case wkbGeometryCollection_mdb:
            return asgeojson_collection(geom, srs, bbox, precision);
        default:
            assert(0);
            return NULL;
    }

    return NULL;
}