Esempio n. 1
0
	Geometry* FeatureShp::CreateGeometry(long index, SHPHandle pSHPHandle)
	{
		if(pSHPHandle==NULL)
		{
			return NULL;
		}

		int	  iWKBLen = 0;
		g_uchar* pWKB = NULL;
		Geometry*	pGeometry  = NULL;
		SHPObject*	pSHPObject = NULL;
		GeometryFactory* pGeometryFactory = augeGetGeometryFactoryInstance();

		pSHPObject = ::SHPReadObject(pSHPHandle, index);
		if(pSHPObject==NULL)
		{
			return NULL;
		}

		iWKBLen = ShpUtil::GetWKBLength(pSHPObject);
		if(iWKBLen==0)
		{
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}

		pWKB = (g_uchar*)auge_malloc(iWKBLen,sizeof(g_uchar));
		if(pWKB==NULL)
		{
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}
		memset(pWKB, 0, iWKBLen);
		if(ShpUtil::SHPObject_2_WKB(pSHPObject, pWKB, iWKBLen)==0)
		{
			delete[] pWKB;
			::SHPDestroyObject(pSHPObject);
			return NULL;
		}

		pGeometry = pGeometryFactory->CreateGeometryFromWKB(pWKB, true);

		if(pGeometry==NULL)
		{
			delete[] pWKB;
		}

		::SHPDestroyObject(pSHPObject);

		return pGeometry;
	}
	void PolygonToLineProcessorImpl::ProcessMultiPolygon(Feature* pinFeature, FeatureClass* poutFeatureClass, FeatureInsertCommand* cmd)
	{
		Geometry* pGeometry = NULL;
		pGeometry = pinFeature->GetGeometry();
		if(pGeometry==NULL)
		{
			return;
		}

		const char* fname = NULL;
		GField*  pField = NULL;
		GFields* pFields = pinFeature->GetFeatureClass()->GetFields();
		g_uint count = pFields->Count();

		GValue* pValue = NULL;
		GValue* pGeoValue = NULL;		
		Feature* poutFeature = poutFeatureClass->NewFeature();

		augeFieldType type = augeFieldTypeNone;
		for(g_uint i=0; i<count; i++)
		{
			pField = pFields->GetField(i);
			type = pField->GetType();
			if(type==augeFieldTypeGeometry)
			{
				continue;
			}

			fname = pField->GetName();
			pValue = pinFeature->GetValue(i);			
			poutFeature->SetValue(fname, pValue);
		}

		Geometry* pGeoLine = NULL;
		GeometryFactory* pGeometryFactory = augeGetGeometryFactoryInstance();
		pField = pinFeature->GetFeatureClass()->GetFields()->GetGeometryField();
		if(pField!=NULL)
		{
			pValue = pinFeature->GetValue(pField->GetName());
			if(pValue!=NULL)
			{
				pGeometry = pValue->GetGeometry();
				if(pGeometry!=NULL)
				{
					g_uint numPoints = 0;
					g_uint numRings = 0;
					g_uint numPolygons = 0;
					g_uint ring_size = 0;
					g_uint line_size = 0;
					LinearRing* pLinearRing = NULL;
					WKBPolygon* pWKBPolygon = NULL;
					WKBMultiPolygon* pWKBMultiPolygon = (WKBMultiPolygon*)pGeometry->AsBinary();
					WKBLineString* pWKBLineString = NULL;
					g_byte* ptr = NULL;

					numPolygons = pWKBMultiPolygon->numPolygons;
					pWKBPolygon = (WKBPolygon*)(&(pWKBMultiPolygon->polygons[0]));
					for(g_uint i=0; i<numPolygons; i++)
					{
						numRings = pWKBPolygon->numRings;
						pLinearRing = (LinearRing*)(&(pWKBPolygon->rings[0]));
						for(g_uint j=0; j<numRings; j++)
						{
							numPoints = pLinearRing->numPoints;
							ring_size = sizeof(auge::Point) * numPoints + sizeof(g_int32);
							line_size = ring_size + sizeof(g_int32) + sizeof(g_byte);

							pWKBLineString = (WKBLineString*)malloc(line_size);
							memset(pWKBLineString, 0, line_size);

							pWKBLineString->byteOrder = coDefaultByteOrder;
							pWKBLineString->wkbType = wkbLineString;
							pWKBLineString->numPoints = numPoints;
							memcpy(&(pWKBLineString->points[0]), &(pLinearRing->points[0]), sizeof(auge::Point) * numPoints);

							pGeoLine = pGeometryFactory->CreateGeometryFromWKB((g_byte*)(pWKBLineString), true);
							pGeoValue = new GValue(pGeoLine);
							poutFeature->SetValue(pField->GetName(), pGeoValue);

							cmd->Insert(poutFeature);

							free(pWKBLineString);

							ptr = (g_byte*)pLinearRing;
							pLinearRing = (LinearRing*)(ptr + ring_size);
						}
						pWKBPolygon = (WKBPolygon*)pLinearRing;
					}
				}
			}
		}
		poutFeature->Release();
	}