GDAL:shp文件创建

创建一个只有点要素的shp文件

#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
	OGRRegisterAll();
	const char *pszDriverName = "ESRI Shapefile";
	OGRSFDriver *poDriver;
	poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);//注册OGRSFDriver驱动器

	if (poDriver == NULL)
	{
		cout << pszDriverName << " driver not available." << endl;
		return 0;
	}
	OGRDataSource *poDS;
	poDS = poDriver->CreateDataSource("D:/TEST/point_out2.shp", NULL);//创建shp数据集,如果已存在文件,会报错
	if (poDS == NULL)
	{
		cout << "Creation of point_out.shp file failed." << endl;
		return 0;
	}
	OGRLayer *poLayer;
	poLayer = poDS->CreateLayer("point_out", NULL, wkbPoint, NULL);//创建图层wkbPoint为点图层

	if (poLayer == NULL)
	{
		cout << "Layer creation failed." << endl;
		return 0;
	}
	OGRFieldDefn firstField("faci_code", OFTInteger);//创建字段
	OGRFieldDefn secondField("X", OFTReal);
	OGRFieldDefn thirdField("Y", OFTReal);
	firstField.SetWidth(32);//设置字段长度
	secondField.SetWidth(32);
	thirdField.SetWidth(32);
	poLayer->CreateField(&firstField);//给图层新增字段列表
	poLayer->CreateField(&secondField);
	poLayer->CreateField(&thirdField);

	double x, y;
	int code;
	for (int i = 0; i != 100; ++i)
	{
		code = i + 1;
		x = i;
		y = 100 + i;
		OGRFeature *poFeature;//创建要素指针
		poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//构造函数中加入字段信息
		poFeature->SetField("faci_code", code);//将字段和对应数据进行绑定
		poFeature->SetField("X", x);
		poFeature->SetField("Y", y);
 		OGRPoint pt;//创建点几何类型
 		pt.setX(x);//给点几何类型赋值
 		pt.setY(y);
		poFeature->SetGeometry(&pt);//将当前点的几何类型和对应的要素绑定

		if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)//将要素和图层进行绑定
		{
			cout << "Failed to create feature in shapefile." << endl;
			return 0;
		}
		OGRFeature::DestroyFeature(poFeature);//销毁要素指针
	}
	OGRDataSource::DestroyDataSource(poDS);//最后关闭数据源指针
	system("pause");
	return 0;
}

打开shp

#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;
int main(){
	GDALAllRegister();
	OGRRegisterAll();
	CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
	CPLSetConfigOption("SHAPE_ENCODING", "");
	const char* shpFileName = "D:/分块影像/shp/shp1.shp";
	OGRDataSource *poSrcDS = OGRSFDriverRegistrar::Open(shpFileName);
	if (poSrcDS == NULL){
		cout << shpFileName << "文件打开失败!" << endl;
		return 0;
	}
	int iLayerNumber = poSrcDS->GetLayerCount();
	OGRLayer *poLayer = poSrcDS->GetLayer(0);
	if (poLayer==NULL)
	{
		cout << "获取图层失败!" << endl;
		return 0;
	}
	poLayer->ResetReading();//重置图层,清楚筛选格式
	OGRFeatureDefn *poDefn = poLayer->GetLayerDefn();//定义要素的指针
	int iFieldCount = poDefn->GetFieldCount();//获取要素的字段个数
	for (int i = 0; i < iFieldCount;i++)
	{
		OGRFieldDefn* poField = poDefn->GetFieldDefn(i);//定义字段指针
		cout << poField->GetNameRef() << "\n" << poField->GetFieldTypeName(poField->GetType()) << "\n" << poField->GetWidth() << "\n" << poField->GetPrecision() << endl;
		cout << "--------------------------------------" << endl;
	}
	cout << "要素个数 = "<<poLayer->GetFeatureCount() << endl;
	OGRFeature *poFeature = NULL;
	while ((poFeature = poLayer->GetNextFeature())!=NULL)
	{
		cout << "当前是第" << poFeature->GetFID() << "属性值" << endl;
		for (int i = 0; i < iFieldCount;i++)
		{
			OGRFieldDefn *POFIELD = poDefn->GetFieldDefn(i);
			const char *str = POFIELD->GetNameRef();
			OGRFieldType type_ = POFIELD->GetType();
			cout << poFeature->GetFieldAsString(i) << endl;
		}
		OGRGeometry *poGeometry = poFeature->GetGeometryRef();
		char*GeoJson = poGeometry->exportToJson();//导出到JSON文件
		char*GeoWkt;
		poGeometry->exportToWkt(&GeoWkt);//导出格式为WKT
		
		const char* nameGeo = poGeometry->getGeometryName();//获得几何体名称
		OGRwkbGeometryType GeoType = poGeometry->getGeometryType();//获得几何体类型
		//写出--------------------------------------------------
		FILE *fp;
		fp = fopen("D:/分块影像/shp/Jack.txt", "w");
		fwrite(GeoWkt, strlen(GeoWkt) + 1, 1, fp);
		fclose(fp);
	}
	system("pause");
	return 0;
}
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
 
GDALAllRegister();
 
    const char * pFileName = "shp.tif";
    const char * pszDriverName = "ESRI Shapefile";
    GDALDriver * poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
    if(poDriver == NULL)
        return ;
    GDALDataset * poDs = poDriver->Create(pFileName, 0,0,0, GDT_Unknown, NULL);
    if(poDs == NULL)
        return ;
 
    //wkbPolygon表示绘制多边形;wkbLinearRing表示绘制线环
    OGRLayer * poLayer = poDs->CreateLayer("ring", NULL, wkbLineString, NULL);//不删
    if(poLayer == NULL)
    {
        return;
    }
 
 
    OGRFeature * poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//放到DestroyFeature删
    //加入几何图形
 
    //例1,绘制线环,内部不标记颜色.线的绘制原则是,从第一个点开始,逐次绘制直到最后一个点。
    //对于闭合的线环,由于首点和尾点是一个点。所以4边形绘制需要5个点,首点被重复一次
    OGRLineString line;
    line.setNumPoints(5);
    line.setPoint(0, 0, 1000, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
    line.setPoint(1, 0, 0, 0);
    line.setPoint(2, 1000, 0, 0);
    line.setPoint(3, 1000, 1000, 0);
    line.setPoint(4, 0, 1000, 0);
 
 
    poFeature->SetGeometry(&line);
 
 
 
    //例2,绘制多边形。多边形内部亦被颜色标记
    /*OGRPolygon poly;
    std::vector<QPoint> vec;
    vec.push_back(QPoint(0,1));
    vec.push_back(QPoint(1,1));
    vec.push_back(QPoint(1,0));
    vec.push_back(QPoint(0,0));
    OGRLinearRing ring;
    for(int i = 0; i < 4; i++)
    {
        ring.addPoint(vec[i].x(), vec[i].y());
    }
    ring.closeRings();
    poly.addRing(&ring);
    poFeature->SetGeometry(&poly);*/
 
    //例3 多个线
    /*OGRMultiLineString multiLine;
    OGRLineString line1, line2;
        line1.setNumPoints(5);
        line1.setPoint(0, 0, 1000, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
        line1.setPoint(1, 0, 0, 0);
        line1.setPoint(2, 1000, 0, 0);
        line1.setPoint(3, 1000, 1000, 0);
        line1.setPoint(4, 0, 1000, 0);
        line2.setNumPoints(5);
        line2.setPoint(0, 500, 1500, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
        line2.setPoint(1, 500, 500, 0);
        line2.setPoint(2, 1500, 500, 0);
        line2.setPoint(3, 1500, 1500, 0);
        line2.setPoint(4, 500, 1500, 0);
    multiLine.addGeometry((const OGRGeometry *)&line1);
    multiLine.addGeometry((const OGRGeometry *)&line2);
    poFeature->SetGeometry(&multiLine);*/
 
    if(poLayer->CreateFeature(poFeature) != OGRERR_NONE)
    {
        return;
    }
 
    OGRFeature::DestroyFeature(poFeature);
    GDALClose(poDs);
    OGRCleanupAll();

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用GDAL库中的Python绑定来进行shp文件的投影转换。首先,确保你已经安装了GDAL库。然后,按照以下步骤进行操作: 1. 导入必要的库: ```python from osgeo import ogr, osr ``` 2. 打开原始的shp文件: ```python source = ogr.Open('path/to/source.shp') layer = source.GetLayer() ``` 3. 创建一个新的shp文件作为输出: ```python driver = ogr.GetDriverByName('ESRI Shapefile') output = driver.CreateDataSource('path/to/output.shp') outLayer = output.CreateLayer('output', geom_type=ogr.wkbPolygon) ``` 4. 定义原始坐标系和目标坐标系: ```python sourceSR = layer.GetSpatialRef() targetSR = osr.SpatialReference() targetSR.ImportFromEPSG(targetEPSG) # 替换targetEPSG为你想要的目标EPSG代码 ``` 5. 创建一个坐标转换器: ```python transform = osr.CoordinateTransformation(sourceSR, targetSR) ``` 6. 遍历原始图层中的要素,并进行投影转换: ```python for feature in layer: geom = feature.GetGeometryRef() geom.Transform(transform) # 创建新要素并将转换后的几何体添加到新图层中 newFeature = ogr.Feature(outLayer.GetLayerDefn()) newFeature.SetGeometry(geom) outLayer.CreateFeature(newFeature) newFeature = None source = None output = None ``` 这样,你就可以将原始shp文件中的几何体投影到目标坐标系,并保存为新的shp文件。 注意:在代码中,将`path/to/source.shp`和`path/to/output.shp`替换为你实际的文件路径,将`targetEPSG`替换为你想要的目标EPSG代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值