灾害评估项目开发(二)shp矢量导入转换

将shp矢量转换为GeoStar格式矢量

bool CMymdbToshp::shp2sketchlayer(CString shpPath, sign::IGeoVectorPtr& pVector,CString layername,int colorIndex,CString Wkt)
{
    USES_CONVERSION;
    //判断
    if(shpPath.IsEmpty() || pVector==NULL)
        return false;
    try
    {
        long lSubCount = pVector->SubLayerCount();
        if( lSubCount<=0 )
            return false;

        CLabelLayer *pCurLyr = NULL;
        if( !layername.IsEmpty() )
        {            
            pCurLyr = GetLabelLayer(pVector, layername);
            if( pCurLyr==NULL )
                return false;
        }

        OGRDataSource* pDataSource = OGRSFDriverRegistrar::Open(shpPath, false);
        if( pDataSource == NULL )
            return false;

        if( pDataSource->GetLayerCount()<=0 )
        {
            OGRDataSource::DestroyDataSource(pDataSource);
            return false;
        }
        
        bool bResult = false;
        int nCount = pDataSource->GetLayerCount();
        for( int i=0; i<pDataSource->GetLayerCount(); i++ )
        {
            OGRLayer* poLayer = pDataSource->GetLayer(i);
            if( poLayer==NULL )
                continue;

            if( pCurLyr==NULL )
            {                        
                pCurLyr = CreateLabelLayer(pVector, A2CT(poLayer->GetLayerDefn()->GetName()), poLayer);
                if( pCurLyr==NULL )
                {
                    pCurLyr = CreateLabelLayer(pVector, "临时操作层", poLayer);
                }
                //    continue;
            }


            bResult = FromOGRLayer(poLayer, pCurLyr,colorIndex,Wkt);
        return bResult;
    }
    catch(...)
    {
        return false;
    }
}
bool CMymdbToshp::FromOGRLayer(OGRLayer* poLayer, CLabelLayer* pLayer,int pColorIndex,CString Wkt)
{
	if( poLayer==NULL || pLayer==NULL )
		return false;

	CString strLayerName = A2CT(poLayer->GetLayerDefn()->GetName());

	geo::IFeatureClassPtr spFeatureClass = pLayer->GetFeatureClass();
	OGRFeature *poFeature = NULL;
	poLayer->ResetReading();
	//获取属性TAG字段
	OGRFeatureDefn *poFDefn=poLayer->GetLayerDefn();
	int iField = poFDefn->GetFieldIndex("TAG");
	

	int size = poLayer->GetFeatureCount();
//	#pragma omp parallel for
	while( (poFeature=poLayer->GetNextFeature()) != NULL )
	{

		int itag = 1;
		OGRFieldDefn *poFieldDefn=poFDefn->GetFieldDefn(iField);
		if(iField != -1)
		{
			if(poFieldDefn->GetType()==OFTInteger)
			{
				itag = poFeature->GetFieldAsInteger(iField);
			}
		}

		OGRwkbGeometryType type = poFeature->GetGeometryRef()->getGeometryType();
		CLabelObject* pGeometry = NULL;
		if(type == wkbPolygon || type == wkbPoint || type == wkbLineString)
		{
			pGeometry = FromOGRGeometry(poFeature->GetGeometryRef(),Wkt);
			if( pGeometry==NULL )
			{
				OGRFeature::DestroyFeature( poFeature );
				continue;
			}

			pGeometry->ReCalculate();
			// 设置渲染属性
			{
				long lCount = pGeometry->GetDisplayStyleCount();
				LINESTYLE *pLineStyle = NULL;
				FILLSTYLE *pFillStyle = NULL;
				POINTSTYLE *pPointStyle = NULL;
				long *pStyle = NULL;
				int iType(-1);
				string strDesc;
				int j =0;
				for( j=0; j<lCount; ++j )
				{
					pGeometry->GetDisplayStyle(j, pStyle, iType, strDesc);
					switch(iType)
					{
					case disp_line:
						{
							pLineStyle = (LINESTYLE*)pStyle;
							if (pColorIndex==0)
							{
								//green
								pLineStyle->color = RGB(0,255,0);
							}
							else if (pColorIndex==1)
							{
								//red
								pLineStyle->color = RGB(255,0,0);
							}
							else if (pColorIndex==2)
							{
								//blue
								pLineStyle->color = RGB(0,0,255);
							}
							else if (pColorIndex==3)
							{
								pLineStyle->color = RGB(0,255,255);
							}
							else if (pColorIndex==4)
							{
								pLineStyle->color = RGB(255,128,128);
							}
							else if (pColorIndex==5)
							{
								pLineStyle->color = RGB(255,255,0);
							}
							else if (pColorIndex==6)
							{
								pLineStyle->color = RGB(255,128,0);
							}
							break;
						}
					case disp_fill:
						{
							pFillStyle = (FILLSTYLE*)pStyle;
							pFillStyle->type= 6;
							break;
						}
					case disp_point:
						{
							pPointStyle = (POINTSTYLE *)pStyle;
							pPointStyle->symbolID = 94;
							pPointStyle->size = 1.5;
							break;
						}
					}
				}
			}
			pGeometry->SetCodeID(itag);
			if( !pLayer->AppendObject(pGeometry) )
			{
				OGRFeature::DestroyFeature( poFeature );
				continue;
			}
			OGRFeature::DestroyFeature( poFeature );
	
			
		}
		else if(type == wkbMultiPolygon)
		{
			OGRGeometry *pGeo=poFeature->GetGeometryRef();
			OGRMultiPolygon *pMulPolygon=(OGRMultiPolygon*)pGeo;
			OGRPolygon *pPolygon=NULL;
			int size = pMulPolygon->getNumGeometries();
			for(int i=0;i<pMulPolygon->getNumGeometries();i++)
			{
				pPolygon=(OGRPolygon*)pMulPolygon->getGeometryRef(i);

				OGRLineString* poLineString = pPolygon->getExteriorRing();

				CGeoRing*    pGeoRing = new CGeoRing();
				if( pGeoRing==NULL )
				{
					OGRFeature::DestroyFeature( poFeature );
					continue;
				}

				int isize = poLineString->getNumPoints();
				for(int i=0;i<poLineString->getNumPoints();i++)
				{
					OGRPoint ogrPoint;
					poLineString->getPoint(i, &ogrPoint);
					double xx = ogrPoint.getX();
					double yy = ogrPoint.getY();
					if((abs(xx) < 180) && (abs(yy) < 90) && (!Wkt.IsEmpty()))
					{
						double dx,dy;
						ConvertCoordPoint(STR_WKT_WGS84,xx,yy,Wkt,&dx,&dy);
						pGeoRing->AddPoint(dx,dy);
					}
					else
						pGeoRing->AddPoint(xx,yy);

				}
				pGeoRing->RecalcBound();
				pGeometry = pGeoRing;

				pGeometry->ReCalculate();
				// 设置渲染属性
				{
					long lCount = pGeometry->GetDisplayStyleCount();
					LINESTYLE *pLineStyle = NULL;
					FILLSTYLE *pFillStyle = NULL;
					long *pStyle = NULL;
					int iType(-1);
					string strDesc;
					int j =0;
					for( j=0; j<lCount; ++j )
					{
						pGeometry->GetDisplayStyle(j, pStyle, iType, strDesc);
						switch(iType)
						{
						case disp_line:
							{
								pLineStyle = (LINESTYLE*)pStyle;
								if (pColorIndex==0)
								{
									//green
									pLineStyle->color = RGB(0,255,0);
								}
								else if (pColorIndex==1)
								{
									//red
									pLineStyle->color = RGB(255,0,0);
								}
								else if (pColorIndex==2)
								{
									//blue
									pLineStyle->color = RGB(0,0,255);
								}
								else if (pColorIndex==3)
								{
									pLineStyle->color = RGB(0,255,255);
								}
								else if (pColorIndex==4)
								{
									pLineStyle->color = RGB(255,128,128);
								}
								else if (pColorIndex==5)
								{
									pLineStyle->color = RGB(255,255,0);
								}
								else if (pColorIndex==6)
								{
									pLineStyle->color = RGB(255,128,0);
								}
								break;
							}
						case disp_fill:
							{
								pFillStyle = (FILLSTYLE*)pStyle;
								pFillStyle->type= 6;
								break;
							}
						}
					}
				}

				if( !pLayer->AppendObject(pGeometry) )
				{
					OGRFeature::DestroyFeature( poFeature );
					continue;
				}
				if( spFeatureClass==NULL || pGeometry->GetOID()<0 )
				{
					OGRFeature::DestroyFeature( poFeature );
					continue;
				}


				pGeometry->SetCodeID(itag);
				if( !pLayer->AppendObject(pGeometry) )
				{
					OGRFeature::DestroyFeature( poFeature );
					continue;
				}

			}
			OGRFeature::DestroyFeature( poFeature );
		}
		
		
	}	
	return true;
}

 

CLabelObject* CMymdbToshp::FromOGRGeometry(OGRGeometry* poGeometry,CString Wkt)
{
	if( poGeometry==NULL )
		return NULL;

	OGRwkbGeometryType type = poGeometry->getGeometryType();
	switch( type )
	{
	case wkbPoint:
		return FromOGRPoint(poGeometry,Wkt);
		break;

	case wkbLineString:
		return FromOGRPolyline(poGeometry,Wkt);
		break;

	case wkbPolygon:
		return FromOGRPolygon(poGeometry,Wkt);
		break;

	default:
		break;
	}

	return NULL;
}

 

CLabelObject* CMymdbToshp::FromOGRPolygon(OGRGeometry* poGeometry,CString Wkt)
{
	if( poGeometry==NULL )
		return NULL;

	OGRPolygon*    poPolygon    = (OGRPolygon*)poGeometry;
	OGRLineString* poLineString = poPolygon->getExteriorRing();

	CGeoRing*    pGeoRing = new CGeoRing();
	//CGeoPolygon* pGeoPolygon = new CGeoPolygon();

	for(int i=0;i<poLineString->getNumPoints();i++)
	{
		OGRPoint ogrPoint;
		poLineString->getPoint(i, &ogrPoint);
		double xx = ogrPoint.getX();
		double yy = ogrPoint.getY();
		if((abs(xx) < 180) && (abs(yy) < 90) && (!Wkt.IsEmpty()))
		{
			double dx,dy;
			ConvertCoordPoint(STR_WKT_WGS84,xx,yy,Wkt,&dx,&dy);
			pGeoRing->AddPoint(dx,dy);
		}
		else
			pGeoRing->AddPoint(xx,yy);

	}

	pGeoRing->RecalcBound();
	//pGeoPolygon->AddRing(pGeoRing);
	//pGeoPolygon->RecalcBound();
	return pGeoRing;
}
CLabelObject* CMymdbToshp::FromOGRPoint(OGRGeometry* poGeometry,CString Wkt)
{
	if( poGeometry==NULL )
		return NULL;

	OGRPoint* poPoint = (OGRPoint*)poGeometry;
	double xx = poPoint->getX();
	double yy = poPoint->getY();
	CGeoPoint* pPoint = new CGeoPoint();
	if((abs(xx) < 180) && (abs(yy) < 90) && (!Wkt.IsEmpty()))
	{
		double dx,dy;
		ConvertCoordPoint(STR_WKT_WGS84,xx,yy,Wkt,&dx,&dy);
		pPoint->SetPoint(dx, dy);
	}
	else
		pPoint->SetPoint(poPoint->getX(), poPoint->getY());
	pPoint->RecalcBound();
	return pPoint;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值