asp.net c# gdal的配置

    xiaoxiao2022-07-03  158

    为了实现arcgis for js中绘制graphic导出shp文件,进行了asp.net 中gdal的配置。 参考的两篇博文 1:https://blog.csdn.net/mygisforum/article/details/22478491,这篇博文时讲了c# 窗体应用的配置,有人参考这个博文的话进行c#配置的话,那个下载的gdal文件其实没有必要,因为那个是博主对应版本的gdal文件,自己下载的gdal版本不一样的话,需要重新按照他的gdal文件里面二级目录配置一下自己的

    2:https://blog.csdn.net/vic_black/article/details/80888477,这篇博文在上篇的基础上进一步进行asp.net的配置。但是某些步骤跳过了。

    1.第一步下载 已经编译好的相应版本的gdal,下载地址:http://www.gisinternals.com/,我下载的版本是release-1700-gdal-2-2-3-mapserver-7-0-7,vs2012 32位 2:下载完解压,并将目录移动到合适位置(不要包含中文或者空格) 3.右键点击SDKShell.bat,选择以管理员身份运行 4.首先bin目录内的dll文件复制到bin\gdal\apps目录下 5.将bin目录下的DLL以及bin\gdal\csharp下的DLL文件均拷贝到项目的bin目录下 6.关闭SDKShell.bat 7.打开VS->打开解决方案->打开项目->右键点击引用->添加引用->下方选择浏览->打开的对话框内切换到项目的bin目录(或debug\x86)多选gdal_csharp.dll、gdalconst_csharp.dll、ogr_csharp.dll、osr_csharp.dll并点击确定关闭对话框->点击确定关闭添加引用窗口。点开引用可见dll。 8.将博文1中下载的GdalConfiguration.cs添加到项目中 9.配置webconfig,主要是要对GdalConfiguration.cs中的gdalData和projSharePath进行配置,配置地址位: 然后将GdalConfiguration.cs中的dalData和projSharePath替换为 ConfigurationManager.AppSettings[“GDAL_DATA”].ToString(); ConfigurationManager.AppSettings[“GDAL_SHARE”].ToString();

    10.用一段代码测试, 11.其中 OSGeo.GDAL.Gdal.SetConfigOption(“GDAL_FILENAME_IS_UTF8”, “YES”); OSGeo.GDAL.Gdal.SetConfigOption(“SHAPE_ENCODING”, “CP936”);//使属性表中可以写入中文

    string wkt = “GEOGCS[‘GCS_China_Geodetic_Coordinate_System_2000’,”+ “DATUM[‘D_China_2000’,SPHEROID[‘CGCS2000’,6378137.0,298.257222101]],PRIMEM[‘Greenwich’,0.0],UNIT[‘Degree’,0.0174532925199433]]”; OSGeo.OSR.SpatialReference sr = new OSGeo.OSR.SpatialReference(wkt); Layer oLayer = oDS.CreateLayer(“TestPolygon”, sr, wkbGeometryType.wkbPolygon, null); //加入坐标系

    SharpMap.GdalConfiguration.ConfigureGdal(); SharpMap.GdalConfiguration.ConfigureOgr(); OSGeo.GDAL.Gdal.AllRegister();

    // 为了使属性表字段支持中文,请添加下面这句 OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); string strVectorFile = "D:\\TestPolygon.shp"; string strDriverName = "ESRI Shapefile"; int count = Ogr.GetDriverCount(); Driver oDriver = Ogr.GetDriverByName(strDriverName); if (oDriver == null) { Console.WriteLine("%s 驱动不可用!\n", strVectorFile); return; } // 创建数据源 DataSource oDS = oDriver.CreateDataSource(strVectorFile, null); if (oDS == null) { Console.WriteLine("创建矢量文件【%s】失败!\n", strVectorFile); return; } // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定 string wkt = "GEOGCS['GCS_China_Geodetic_Coordinate_System_2000',"+

    “DATUM[‘D_China_2000’,SPHEROID[‘CGCS2000’,6378137.0,298.257222101]],PRIMEM[‘Greenwich’,0.0],UNIT[‘Degree’,0.0174532925199433]]”;//自定义投影坐标系的WKT OSGeo.OSR.SpatialReference sr = new OSGeo.OSR.SpatialReference(wkt); Layer oLayer = oDS.CreateLayer(“TestPolygon”, sr, wkbGeometryType.wkbPolygon, null); if (oLayer == null) { Console.WriteLine(“图层创建失败!\n”); return; } // 下面创建属性表 // 先创建一个叫FieldID的整型属性 FieldDefn oFieldID = new FieldDefn(“FieldID”, FieldType.OFTInteger); oLayer.CreateField(oFieldID, 1);

    // 再创建一个叫FeatureName的字符型属性,字符长度为50 FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString); oFieldName.SetWidth(100); oLayer.CreateField(oFieldName, 1); FeatureDefn oDefn = oLayer.GetLayerDefn(); // 创建三角形要素 Feature oFeatureTriangle = new Feature(oDefn); oFeatureTriangle.SetField(0, 0); oFeatureTriangle.SetField(1, "sanjiao"); Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))"); oFeatureTriangle.SetGeometry(geomTriangle); oLayer.CreateFeature(oFeatureTriangle); // 创建矩形要素 Feature oFeatureRectangle = new Feature(oDefn); oFeatureRectangle.SetField(0, 1); oFeatureRectangle.SetField(1, "是的juxing"); Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); oFeatureRectangle.SetGeometry(geomRectangle); oLayer.CreateFeature(oFeatureRectangle); // 创建岛要素 Feature oFeatureHole = new Feature(oDefn); oFeatureHole.SetField(0, 1); oFeatureHole.SetField(1, "huandao"); //Geometry geomWYX = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); OSGeo.OGR.Geometry outGeo = new

    OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); outGeo.AddPoint(40, -30, 0); outGeo.AddPoint(60, -30, 0); outGeo.AddPoint(60, -10, 0); outGeo.AddPoint(40, -10, 0);

    OSGeo.OGR.Geometry inGeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); inGeo.AddPoint(45, -25, 0); inGeo.AddPoint(55, -25, 0); inGeo.AddPoint(55, -15, 0); inGeo.AddPoint(45, -15, 0); OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon); geo.AddGeometryDirectly(outGeo); geo.AddGeometryDirectly(inGeo); oFeatureHole.SetGeometry(geo); oLayer.CreateFeature(oFeatureHole); // 创建Multi要素 Feature oFeatureMulty = new Feature(oDefn); oFeatureMulty.SetField(0, 1); oFeatureMulty.SetField(1, "MultyPart"); OSGeo.OGR.Geometry geo1 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); geo1.AddPoint(25, -10, 0); geo1.AddPoint(5, -10, 0); geo1.AddPoint(5, -30, 0); geo1.AddPoint(25, -30, 0); OSGeo.OGR.Geometry poly1 = new Geometry(wkbGeometryType.wkbPolygon); poly1.AddGeometryDirectly(geo1); OSGeo.OGR.Geometry geo2 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); geo2.AddPoint(0, -15, 0); geo2.AddPoint(-5, -15, 0); geo2.AddPoint(-5, -20, 0); geo2.AddPoint(0, -20, 0); OSGeo.OGR.Geometry poly2 = new Geometry(wkbGeometryType.wkbPolygon); poly2.AddGeometryDirectly(geo2);

    OSGeo.OGR.Geometry geoMulty = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbMultiPolygon); geoMulty.AddGeometryDirectly(poly1); geoMulty.AddGeometryDirectly(poly2); oFeatureMulty.SetGeometry(geoMulty); oLayer.CreateFeature(oFeatureMulty);

    11.生成完shp文件后关掉iis服务可以打开查看,这个需要重写一下逻辑

    新手上路,有些名词描述的不准确甚至有错误,欢迎指正交流。

    最新回复(0)