gdal 中实现面或者线转化成点图层


.h中的实现

typedef std::vector<OGRFeature *> Cachevector;
typedef std::tuple<QString,QString,QString> LayerPathInfo;
typedef enum TranForm_enum
{
	tranform_polygontpoint = 0,
	tranform_linetpoint,
}TranForm_enum;
define IF_NULL_RETRUN(p) if(NULL == p) {return;}
define IF_NULL_RETRUN_FALSE(p) if(NULL == p) {return false;}
class TDConversionToPoint
{
public:
	bool polygonToPoint(LayerPathInfo originalPathInfo,LayerPathInfo targetPathInfo);
	bool lineToPoint(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo);
private:
	GDALDataset * m_preadDataset = { NULL };
	GDALDataset * m_pwriteDataset = { NULL };
	OGRLayer * m_armLayer = { NULL };
	Cachevector m_cachevector;
	int catchnum = 10000;
	bool readOriginalInfo(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo, TranForm_enum enum_obj);
	void polygonFeaturetoPoint(OGRFeature *poFeature);
	void lineFeatureToPoint(OGRFeature *poFeature);
	void openWriteDataSet(LayerPathInfo targetPathInfo, OGRFeatureDefn *);
	void wiriteInfo(LayerPathInfo targetPathInfo, OGRFeatureDefn * pogrfeature);
	void tranformToPoint(TranForm_enum enum_obj, OGRFeature *poFeature);

};

CPP 中实现

void TDConversionToPoint::polygonFeaturetoPoint(OGRFeature *poFeature)
{
	IF_NULL_RETRUN(poFeature)
	auto pgeom = poFeature->GetGeometryRef();
	auto fun = [&](OGRLineString *pline){
		auto pointnum = pline->getNumPoints();
		for (int i = 0; i < pointnum; i++)
		{
			OGRPoint * ogrPoint = NULL;
			pline->getPoint(i, ogrPoint);
			OGRFeature *armFeature = poFeature->Clone();
			armFeature->SetGeometry(ogrPoint);
			m_cachevector.push_back(armFeature);
		}
	};
	if (wkbPolygon == pgeom->getGeometryType())
	{
		auto ppolygon = dynamic_cast&lt;OGRPolygon *>(pgeom);
		fun(ppolygon->getExteriorRing());
		auto intnum = ppolygon->getNumInteriorRings();
		for (int i = 0; i < intnum; i++)
		{
			fun(ppolygon->getInteriorRing(i));
		}
	}
}
void TDConversionToPoint::lineFeatureToPoint(OGRFeature *poFeature)
{
	IF_NULL_RETRUN(poFeature)
	auto pgeom = poFeature->GetGeometryRef();
	auto fun = [&](OGRLineString *pline) {
		auto pointnum = pline->getNumPoints();
		for (int i = 0; i < pointnum; i++)
		{
			OGRPoint * ogrPoint = NULL;
			pline->getPoint(i, ogrPoint);
			OGRFeature *armFeature = poFeature->Clone();
			armFeature->SetGeometry(ogrPoint);
			m_cachevector.push_back(armFeature);
		}
	};
	if (wkbLineString == pgeom->getGeometryType())
	{
		auto pLine = dynamic_cast&lt;OGRLineString *>(pgeom);
		fun(pLine);
	}
}
void TDConversionToPoint::openWriteDataSet(LayerPathInfo targetPathInfo, OGRFeatureDefn * pogrfeature)
{
	GDALAllRegister();
	auto writeDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(std::get<2>(targetPathInfo).toStdString().c_str());
	IF_NULL_RETRUN(writeDriver)
	//尝试打开
	m_pwriteDataset = writeDriver->Open(std::get<1>(targetPathInfo).toStdString().c_str());
	if (NULL == m_pwriteDataset)
	{
		m_pwriteDataset = writeDriver->Create(std::get<1>(targetPathInfo).toStdString().c_str(),512,512, 1, GDT_Byte, NULL);
	}
	m_armLayer  = m_pwriteDataset->CreateLayer(std::get<0>(targetPathInfo).toStdString().c_str());
	IF_NULL_RETRUN(writeDriver)
	for (int i = 0; i < pogrfeature->GetFieldCount(); i++)
	{
		m_armLayer->CreateField(pogrfeature->GetFieldDefn(i));
	}
}
void TDConversionToPoint::tranformToPoint(TranForm_enum enum_obj, OGRFeature *poFeature)
{
	if (tranform_polygontpoint == enum_obj)
	{
		polygonFeaturetoPoint(poFeature);
	}
	if (tranform_linetpoint == enum_obj)
	{
		lineFeatureToPoint(poFeature);
	}
}
void TDConversionToPoint::wiriteInfo(LayerPathInfo targetPathInfo,OGRFeatureDefn * pogrfeature)
{
	if (NULL == m_armLayer)
	{
		openWriteDataSet(targetPathInfo, pogrfeature);
	}
	for_each(m_cachevector.begin(), m_cachevector.end(), [&](OGRFeature *pogrfeature) {
		m_armLayer->CreateFeature(pogrfeature);
	});
	m_cachevector.clear();
}
bool TDConversionToPoint::readOriginalInfo(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo, TranForm_enum enum_obj)
{
	GDALAllRegister();
	auto readDriver = (OGRSFDriver *)OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(std::get<2>(originalPathInfo).toStdString().c_str());
	IF_NULL_RETRUN_FALSE(readDriver)
	m_preadDataset = readDriver->Open(std::get<1>(originalPathInfo).toStdString().c_str());
	IF_NULL_RETRUN_FALSE(m_preadDataset)
	auto orgLayer	= m_preadDataset->GetLayerByName(std::get<0>(originalPathInfo).toStdString().c_str());
	IF_NULL_RETRUN_FALSE(orgLayer)
	OGRFeature *poFeature;
	orgLayer->ResetReading();
	OGRFeatureDefn * ogrfeaturedefn = orgLayer->GetLayerDefn();
	int num = 0;
	while ((poFeature = orgLayer->GetNextFeature()) != NULL)
	{
		tranformToPoint(enum_obj,poFeature);
		num++;
		if (num > catchnum)
		{
			wiriteInfo(targetPathInfo, ogrfeaturedefn);
			num = 0;
		}
	}
	wiriteInfo(targetPathInfo, ogrfeaturedefn);
	GDALClose(m_preadDataset);
	GDALClose(m_pwriteDataset);
	return true;
}
bool TDConversionToPoint::polygonToPoint(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo)
{
	return readOriginalInfo(originalPathInfo, targetPathInfo, tranform_polygontpoint);
}
bool TDConversionToPoint::lineToPoint(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo)
{
	return readOriginalInfo(originalPathInfo, targetPathInfo, tranform_linetpoint);
}

转载自:https://blog.csdn.net/u012453032/article/details/81206099

You may also like...