📄 shpgeo.c
字号:
/* ************************************************************************** * SHPReadOGisWKB * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/SHPObject* SHPReadOGisWKB ( WKBStreamObj *stream_obj) { SHPObject *psCShape; char WKB_order; int need_swap = 0, my_order, GeoType; int use_Z = 0, use_M = 0; int nSHPType, thisDim; WKBStreamRead ( stream_obj, &WKB_order, 1, sizeof(char)); my_order = 1; my_order = ((char*) (&my_order))[0]; stream_obj->NeedSwap = !(WKB_order & my_order); /* convert OGis Types to SHP types */ nSHPType = SHPOGisType ( GeoType, 0 ); WKBStreamRead ( stream_obj, &GeoType, 1, sizeof(int)); thisDim = SHPDimension ( nSHPType ); if ( thisDim && SHPD_AREA ) { psCShape = SHPReadOGisPolygon ( stream_obj ); } else { if ( thisDim && SHPD_LINE ) { psCShape = SHPReadOGisLine ( stream_obj ); } else { if ( thisDim && SHPD_POINT ) { psCShape = SHPReadOGisPoint ( stream_obj ); } } } return (0);}/* ************************************************************************** * SHPWriteOGisWKB * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/int SHPWriteOGisWKB ( WKBStreamObj* stream_obj, SHPObject *psCShape ) { int need_swap = 0, my_order, GeoType, thisDim; int use_Z = 0, use_M = 0; char LSB = 1; /* indicate that this WKB is in LSB Order */ /* OGis WKB can handle either byte order, but if I get to choose I'd /* rather have it predicatable system-to-system */ if ( stream_obj ) { if ( stream_obj->wStream ) free ( stream_obj->wStream ); } else { stream_obj = calloc ( 3, sizeof (int ) ); } /* object size needs to be 9 bytes for the wrapper, and for each polygon */ /* another 9 bytes all plus twice the total number of vertices */ /* times the sizeof (double) and just pad with 10 more chars for fun */ stream_obj->wStream = calloc (1, (9 * (psCShape->nParts + 1)) + ( sizeof(double) * 2 * psCShape->nVertices ) + 10 ); #ifdef DEBUG2 printf (" I just allocated %d bytes to wkbObj \n", sizeof (int) + sizeof (int) + sizeof(int) + ( sizeof(int) * psCShape->nParts + 1 ) + ( sizeof(double) * 2 * psCShape->nVertices ) + 10 ); #endif my_order = 1; my_order = ((char*) (&my_order))[0]; /* Need to swap if this system is not LSB (Intel Order) */ stream_obj->NeedSwap = ( my_order != LSB ); stream_obj->StreamPos = 0; #ifdef DEBUG2 printf ("this system is (%d) LSB recorded as needSwap %d\n",my_order, stream_obj->NeedSwap); #endif WKBStreamWrite ( stream_obj, & LSB, 1, sizeof(char) ); #ifdef DEBUG2 printf ("this system in (%d) LSB \n"); #endif /* convert SHP Types to OGis types */ GeoType = SHPOGisType ( psCShape->nSHPType, 1 ); WKBStreamWrite ( stream_obj, &GeoType, 1, sizeof(int) ); thisDim = SHPDimension ( psCShape->nSHPType ); if ( thisDim && SHPD_AREA ) { SHPWriteOGisPolygon ( stream_obj, psCShape ); } else { if ( thisDim && SHPD_LINE ) { SHPWriteOGisLine ( stream_obj, psCShape ); } else { if ( thisDim && SHPD_POINT ) { SHPWriteOGisPoint ( stream_obj, psCShape ); } } }#ifdef DEBUG2 printf("(SHPWriteOGisWKB) outta here when stream pos is %d \n", stream_obj->StreamPos);#endif return (0);}/* ************************************************************************** * SHPWriteOGisPolygon * * for this pass code to more generic OGis MultiPolygon Type * later add support for OGis Polygon Type * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/int SHPWriteOGisPolygon ( WKBStreamObj *stream_obj, SHPObject *psCShape ) { SHPObject **ppsC; SHPObject *psC; int rPart, ring, rVertices, cpart, cParts, nextring, i, j; char Flag = 1; int GeoType = OGIST_POLYGON; /* cant have more than nParts complex objects in this object */ ppsC = calloc ( psCShape->nParts, sizeof(int) ); nextring = 0; cParts=0; while ( nextring >= 0 ) { (SHPObject*) ppsC[cParts] = SHPUnCompound ( psCShape, &nextring ); cParts++; } #ifdef DEBUG2 printf ("(SHPWriteOGisPolygon) Uncompounded into %d parts \n", cParts);#endif WKBStreamWrite ( stream_obj, &cParts, 1, sizeof(int) ); for ( cpart = 0; cpart < cParts; cpart++) { WKBStreamWrite ( stream_obj, & Flag, 1, sizeof(char) ); WKBStreamWrite ( stream_obj, & GeoType, 1, sizeof(int) ); psC = (SHPObject*) ppsC[cpart]; WKBStreamWrite ( stream_obj, &(psC->nParts), 1, sizeof(int) ); for ( ring = 0; (ring < (psC->nParts)) && (psC->nParts > 0); ring ++) { if ( ring < (psC->nParts-2) ) { rVertices = psC->panPartStart[ring+1] - psC->panPartStart[ring]; } else { rVertices = psC->nVertices - psC->panPartStart[ring]; }#ifdef DEBUG2 printf ("(SHPWriteOGisPolygon) scanning part %d, ring %d %d vtxs \n", cpart, ring, rVertices);#endif rPart = psC->panPartStart[ring]; WKBStreamWrite ( stream_obj, &rVertices, 1, sizeof(int) ); for ( j=rPart; j < (rPart + rVertices); j++ ) { WKBStreamWrite ( stream_obj, &(psC->padfX[j]), 1, sizeof(double) ); WKBStreamWrite ( stream_obj, &(psC->padfY[j]), 1, sizeof(double) ); } /* for each vertex */ } /* for each ring */ } /* for each complex part */#ifdef DEBUG2 printf ("(SHPWriteOGisPolygon) outta here \n");#endif return (1);}/* ************************************************************************** * SHPWriteOGisLine * * for this pass code to more generic OGis MultiXXXXXXX Type * later add support for OGis LineString Type * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/int SHPWriteOGisLine ( WKBStreamObj *stream_obj, SHPObject *psCShape ) { return ( SHPWriteOGisPolygon( stream_obj, psCShape ));}/* ************************************************************************** * SHPWriteOGisPoint * * for this pass code to more generic OGis MultiPoint Type * later add support for OGis Point Type * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/int SHPWriteOGisPoint ( WKBStreamObj *stream_obj, SHPObject *psCShape ) { int j; WKBStreamWrite ( stream_obj, &(psCShape->nVertices), 1, sizeof(int) ); for ( j=0; j < psCShape->nVertices; j++ ) { WKBStreamWrite ( stream_obj, &(psCShape->padfX[j]), 1, sizeof(double) ); WKBStreamWrite ( stream_obj, &(psCShape->padfY[j]), 1, sizeof(double) ); } /* for each vertex */ return (1);}/* ************************************************************************** * SHPReadOGisPolygon * * for this pass code to more generic OGis MultiPolygon Type * later add support for OGis Polygon Type * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/SHPObject* SHPReadOGisPolygon ( WKBStreamObj *stream_obj ) { SHPObject **ppsC; SHPObject *psC; int rPart, ring, rVertices, cpart, cParts, nextring, i, j; int totParts, totVertices, pRings, nParts; psC = SHPCreateObject ( SHPT_POLYGON, -1, 0, NULL, NULL, 0, NULL, NULL, NULL, NULL ); /* initialize a blank SHPObject */ WKBStreamRead ( stream_obj, &cParts, 1, sizeof(char) ); totParts = cParts; totVertices = 0; SfRealloc ( psC->panPartStart, cParts * sizeof(int)); SfRealloc ( psC->panPartType, cParts * sizeof(int)); for ( cpart = 0; cpart < cParts; cpart++) { WKBStreamRead ( stream_obj, &nParts, 1, sizeof(int) ); pRings = nParts; /* pRings is the number of rings prior to the Ring loop below */ if ( nParts > 1 ) { totParts += nParts - 1; SfRealloc ( psC->panPartStart, totParts * sizeof(int)); SfRealloc ( psC->panPartType, totParts * sizeof(int)); } rPart = 0; for ( ring = 0; ring < (nParts - 1); ring ++) { WKBStreamRead ( stream_obj, &rVertices, 1, sizeof(int) ); totVertices += rVertices; psC->panPartStart[ring+pRings] = rPart; if ( ring == 0 ) { psC->panPartType[ring + pRings] = SHPP_OUTERRING; } else { psC->panPartType[ring + pRings] = SHPP_INNERRING; } SfRealloc ( psC->padfX, totVertices * sizeof (double)); SfRealloc ( psC->padfY, totVertices * sizeof (double)); for ( j=rPart; j < (rPart + rVertices); j++ ) { WKBStreamRead ( stream_obj, &(psC->padfX[j]), 1, sizeof(double) ); WKBStreamRead ( stream_obj, &(psC->padfY[j]), 1, sizeof(double) ); } /* for each vertex */ rPart += rVertices; } /* for each ring */ } /* for each complex part */ return ( psC ); }/* ************************************************************************** * SHPReadOGisLine * * for this pass code to more generic OGis MultiLineString Type * later add support for OGis LineString Type * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/SHPObject* SHPReadOGisLine ( WKBStreamObj *stream_obj ) { SHPObject **ppsC; SHPObject *psC; int rPart, ring, rVertices, cpart, cParts, nextring, i, j; int totParts, totVertices, pRings, nParts; psC = SHPCreateObject ( SHPT_ARC, -1, 0, NULL, NULL, 0, NULL, NULL, NULL, NULL ); /* initialize a blank SHPObject */ WKBStreamRead ( stream_obj, &cParts, 1, sizeof(int) ); totParts = cParts; totVertices = 0; SfRealloc ( psC->panPartStart, cParts * sizeof(int)); SfRealloc ( psC->panPartType, cParts * sizeof(int)); for ( cpart = 0; cpart < cParts; cpart++) { WKBStreamRead ( stream_obj, &nParts, 1, sizeof(int) ); pRings = totParts; /* pRings is the number of rings prior to the Ring loop below */ if ( nParts > 1 ) { totParts += nParts - 1; SfRealloc ( psC->panPartStart, totParts * sizeof(int)); SfRealloc ( psC->panPartType, totParts * sizeof(int)); } rPart = 0; for ( ring = 0; ring < (nParts - 1); ring ++) { WKBStreamRead ( stream_obj, &rVertices, 1, sizeof(int) ); totVertices += rVertices; psC->panPartStart[ring+pRings] = rPart; if ( ring == 0 ) { psC->panPartType[ring + pRings] = SHPP_OUTERRING; } else { psC->panPartType[ring + pRings] = SHPP_INNERRING; } SfRealloc ( psC->padfX, totVertices * sizeof (double)); SfRealloc ( psC->padfY, totVertices * sizeof (double)); for ( j=rPart; j < (rPart + rVertices); j++ ) { WKBStreamRead ( stream_obj, &(psC->padfX[j]), 1, sizeof(double) ); WKBStreamRead ( stream_obj, &(psC->padfY[j]), 1, sizeof(double) ); } /* for each vertex */ rPart += rVertices; } /* for each ring */ } /* for each complex part */ return ( psC );}/* ************************************************************************** * SHPReadOGisPoint * * Encapsulate entire SHPObject for use with Postgresql * * **************************************************************************/SHPObject* SHPReadOGisPoint ( WKBStreamObj *stream_obj ) { SHPObject *psC; int nVertices, j; psC = SHPCreateObject ( SHPT_MULTIPOINT, -1, 0, NULL, NULL, 0, NULL, NULL, NULL, NULL ); /* initialize a blank SHPObject */ WKBStreamRead ( stream_obj, &nVertices, 1, sizeof(int) ); SfRealloc ( psC->padfX, nVertices * sizeof (double)); SfRealloc ( psC->padfY, nVertices * sizeof (double)); for ( j=0; j < nVertices; j++ ) { WKBStreamRead ( stream_obj, &(psC->padfX[j]), 1, sizeof(double) ); WKBStreamRead ( stream_obj, &(psC->padfY[j]), 1, sizeof(double) ); } /* for each vertex */ return ( psC );}/* ************************************************************************** * RingReadOGisWKB * * this accepts OGisLineStrings which are basic building blocks *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -