gdal 将点转化成三角网格,泰森多边形生成

类的定义```

class TDOverlayerAnalysisTriangularmesh : public TDconversionBase {
 
public:
	bool triangularmesh(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo);
private:
	bool readOriginalInfo(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo, TranForm_enum enum_obj = tranform_save);
	void tranformToPoint(TranForm_enum enum_obj, OGRFeature *poFeature) override;
	std::vector<OGRFeature *> m_orgdatainfo;
	void calarminfo();
	void calarmVoronoiDiagram();
};

实现转化
//调用geos生成泰森多边形

void TDOverlayerAnalysisTriangularmesh::calarmVoronoiDiagram()
{
VoronoiDiagramBuilder voronoiDiagramBuilder;
CoordinateArraySequence coords;
Envelope clipEnvelpoe;
std::for_each(m_orgdatainfo.begin(), m_orgdatainfo.end(), [&](OGRFeature *p) {
auto ppo = (OGRPoint *)(p->GetGeometryRef());
Coordinate coord(ppo->getX(), ppo->getY());
coords.add(coord);
clipEnvelpoe.expandToInclude(coord);
});
voronoiDiagramBuilder.setSites(coords);
voronoiDiagramBuilder.setClipEnvelope(&clipEnvelpoe);
auto vecgeom = voronoiDiagramBuilder.getDiagram(*GeometryFactory::getDefaultInstance());
for (int i = 0; i < vecgeom->getNumGeometries(); i++)
{
auto singlegeom = vecgeom->getGeometryN(i);
WKBWriter wkbwriter;
std::ostringstream ostr;
wkbwriter.write(*singlegeom, ostr);
OGRPolygon *armtemp = new OGRPolygon();
std::string  gstr = ostr.str();
int atoput;
armtemp->importFromWkb((unsigned char *)gstr.c_str(),-1, wkbVariantOldOgc, atoput);
auto armfeature = m_orgdatainfo[i]->Clone();
armfeature->SetGeometry(armtemp);
m_cachevector.push_back(armfeature);
}
}

//三角网格转化

void TDOverlayerAnalysisTriangularmesh::calarminfo()
{
double *dafx= new double[m_orgdatainfo.size()];
double *dafy = new double[m_orgdatainfo.size()];
int num = 0;
std::for_each(m_orgdatainfo.begin(), m_orgdatainfo.end(),[&](OGRFeature *p){
auto ppo = (OGRPoint *)(p->GetGeometryRef());
dafx[num] = ppo->getX();
dafy[num] = ppo->getY();
num++;
});
auto triangularmesh = GDALTriangulationCreateDelaunay(m_orgdatainfo.size(),dafx, dafy);
for (int i = 0; i < triangularmesh->nFacets; i++)
{
int indexone = triangularmesh->pasFacets[i].anVertexIdx[0];
int indextwo = triangularmesh->pasFacets[i].anVertexIdx[1];
int indexthress = triangularmesh->pasFacets[i].anVertexIdx[2];
auto point1 = m_orgdatainfo[indexone]->GetGeometryRef()->toPoint();
auto point2 = m_orgdatainfo[indextwo]->GetGeometryRef()->toPoint();
auto point3 = m_orgdatainfo[indexthress]->GetGeometryRef()->toPoint();
OGRTriangle *geom = new OGRTriangle(*point1, *point2, *point3);
auto armfeature = m_orgdatainfo[indexone]->Clone();
armfeature->SetGeometry(geom);
m_cachevector.push_back(armfeature);
}
delete[] dafx;
delete[] dafy;
GDALTriangulationFree(triangularmesh);
}
bool TDOverlayerAnalysisTriangularmesh::readOriginalInfo(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo, TranForm_enum enum_obj)
{
(void)openOriginalInfo(originalPathInfo);
IF_NULL_RETRUN_FALSE(m_orgLayer)
OGRFeature *poFeature;
m_orgLayer->ResetReading();
OGRFeatureDefn * ogrfeaturedefn = m_orgLayer->GetLayerDefn();
pmoSourceSRS = m_orgLayer->GetSpatialRef();
while ((poFeature = m_orgLayer->GetNextFeature()) != NULL)
{
tranformToPoint(enum_obj, poFeature);
//OGRFeature::DestroyFeature(poFeature);
}
//calarminfo();
calarmVoronoiDiagram();
wiriteInfo(targetPathInfo, ogrfeaturedefn);
m_cachevector.clear();
if (NULL != m_pwriteDataset)
{
GDALClose(m_pwriteDataset);
}
if (NULL != m_preadDataset)
{
GDALClose(m_preadDataset);
}
return true;
}
bool TDOverlayerAnalysisTriangularmesh::triangularmesh(LayerPathInfo originalPathInfo, LayerPathInfo targetPathInfo)
{
return readOriginalInfo(originalPathInfo, targetPathInfo, transform_delaunaytriangulation);
}
void TDOverlayerAnalysisTriangularmesh::tranformToPoint(TranForm_enum enum_obj, OGRFeature *poFeature)
{

IF_NULL_RETRUN(poFeature)
auto pgeom = poFeature->GetGeometryRef();
if (NULL != pgeom && pgeom->getGeometryType() == wkbPoint)
{
m_orgdatainfo.push_back(poFeature);
}
}`这里写代码片`
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要将Shapefile格式文件转换为DWG格式,你可以使用GDAL库中的OGR(OpenGIS Simple Features Reference Implementation)功能。下面是使用GDAL进行转换的基本步骤: 1. 引用GDAL和OGR库:在C#项目中,首先需要引用GDAL和OGR库。你可以在代码文件中添加以下引用: ```csharp using OSGeo.GDAL; using OSGeo.OGR; ``` 2. 初始化GDAL:在程序的入口点或初始化代码中,调用GDAL的初始化方法: ```csharp Gdal.AllRegister(); ``` 3. 打开Shapefile数据源:使用OGR打开Shapefile数据源: ```csharp DataSource sourceDS = Ogr.Open("path/to/your/shapefile", 0); ``` 4. 创建DWG文件数据源:使用OGR创建DWG文件数据源: ```csharp Driver dwgDriver = Ogr.GetDriverByName("DWG"); DataSource targetDS = dwgDriver.CreateDataSource("path/to/your/dwgfile", null); ``` 5. 复制图层:遍历Shapefile数据源中的图层,并将其复制到DWG数据源中: ```csharp for (int i = 0; i < sourceDS.GetLayerCount(); i++) { Layer sourceLayer = sourceDS.GetLayerByIndex(i); Layer targetLayer = targetDS.CreateLayer(sourceLayer.GetName(), sourceLayer.GetSpatialRef(), sourceLayer.GetGeomType(), null); FeatureDefn featureDefn = sourceLayer.GetLayerDefn(); for (int j = 0; j < featureDefn.GetFieldCount(); j++) { FieldDefn fieldDefn = featureDefn.GetFieldDefn(j); targetLayer.CreateField(fieldDefn, 1); } sourceLayer.ResetReading(); Feature feature; while ((feature = sourceLayer.GetNextFeature()) != null) { Feature targetFeature = new Feature(featureDefn); targetFeature.SetFrom(feature); targetLayer.CreateFeature(targetFeature); feature.Dispose(); targetFeature.Dispose(); } sourceLayer.Dispose(); targetLayer.Dispose(); } ``` 6. 关闭数据源:在使用完数据后,需要关闭数据源以释放资源: ```csharp sourceDS.Dispose(); targetDS.Dispose(); ``` 以上是使用GDAL库将Shapefile格式文件转换为DWG格式的基本步骤。请注意,转换过程中可能需要进行投影转换或其他额外的操作,具体取决于你的数据和需求。你可以根据需要进行进一步的处理和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值