public static HandleRef getCPtrAndSetReference(Layer obj, object parent) { if (obj != null) { obj.swigParentRef = parent; return obj.swigCPtr; } else { return new HandleRef(null, IntPtr.Zero); } }
async static void subAdd(Feature poiFeat, string dzx, Layer newLayer) { await Task.Run(() => { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzxLayer = dzxDS.GetLayerByIndex(0); int dzxCount = dzxLayer.GetFeatureCount(0); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int i = 0; i < dzxLayer.GetFeatureCount(0); i++) { OSGeo.OGR.Feature dzxFeat = dzxLayer.GetFeature(i); OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef(); if (poiGeom.Within(dzxGeom)) { newLayer.CreateFeature(dzxFeat); } dzxGeom.Dispose(); dzxFeat.Dispose(); } poiFeat.Dispose(); dzxDS.Dispose(); tickTime++; }); }
public IArray get_DatasetNames(esriDatasetType DatasetType) { if (m_connString == null) { return(null); } if (DatasetType == esriDatasetType.esriDTAny || DatasetType == esriDatasetType.esriDTFeatureClass) { IArray datasets = new ArrayClass(); int count = m_datasource.GetLayerCount(); for (int i = 0; i < count; i++) { OSGeo.OGR.Layer layer = m_datasource.GetLayerByIndex(i); OGRDataset dataset = new OGRDataset(layer); datasets.Add(dataset); } return(datasets); } return(null); }
/// <summary> /// 整合坡度线(Line) /// </summary> /// <param name="subPdx"></param> /// <returns></returns> public static string zhengHePdLine(List <string> subPdx) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); string newPath = StaticTools.tempFilePath("shp", "整合线"); OSGeo.OGR.DataSource newDs = dr.CreateDataSource(newPath, null);///////////////// OSGeo.OGR.Layer newLayer = newDs.CreateLayer("Lines", dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetSpatialRef(), dr.Open(subPdx[0], 0).GetLayerByIndex(0).GetGeomType(), null); for (int i = 0; i < subPdx.Count; i++) { OSGeo.OGR.DataSource dsLine = dr.Open(subPdx[i], 0);/////////////////////// OSGeo.OGR.Layer lineLayer = dsLine.GetLayerByIndex(0); for (int j = 0; j < lineLayer.GetFeatureCount(0); j++) { newLayer.CreateFeature(lineLayer.GetFeature(j)); } dsLine.Dispose(); } newDs.Dispose(); return(newPath); }
public static string ToGeoJson(string shpPath) { OSGeo.OGR.Ogr.RegisterAll(); Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); using (var ds = drv.Open(shpPath, 0)) { OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.Feature f; layer.ResetReading(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); while ((f = layer.GetNextFeature()) != null) { var geom = f.GetGeometryRef(); if (geom != null) { var geometryJson = geom.ExportToJson(null); sb.AppendLine(geometryJson); } } return(sb.ToString()); } }
/// <summary> /// 清理重复的Featuer----------多线程----与单线程效率差别很小,不推荐 /// </summary> /// <param name="filePath"></param> public static void cleanPdxM(string filePath) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource ds = dr.Open(filePath, 1); OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); int featCount = layer.GetFeatureCount(0); var sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < featCount - 1; i++) { getDoubFeat(filePath, i, featCount); } while (tickTime != featCount - 1) { Thread.Sleep(2000); Console.WriteLine(tickTime.ToString() + " /" + featCount.ToString() + "/ deletePoint " + ids.Count().ToString()); } sw.Stop(); Console.WriteLine("多线程清理重复Featuer用时:" + sw.Elapsed.ToString()); for (int i = 0; i < ids.Count; i++) { layer.DeleteFeature(ids[i]); } string a = "REPACK " + layer.GetName(); ds.ExecuteSQL(a, null, ""); ds.Dispose(); }
async static void getDoubFeat(string filePath, int s, int ss) { await Task.Run(() => { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource ds = dr.Open(filePath, 0); OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.Feature oriFeat = layer.GetFeature(s); for (int i = s + 1; i < ss; i++) { OSGeo.OGR.Feature nextFeat = layer.GetFeature(i); if (StaticTools.isSame(oriFeat, nextFeat, 1)) { ids.Add(s); break; } nextFeat.Dispose(); } oriFeat.Dispose(); layer.Dispose(); ds.Dispose(); tickTime++; } ); }
public override Layer Open(string pLayerName) { // Setup driver Driver drv = Ogr.GetDriverByName("GML"); if (drv == null) { Debug.WriteLine("Could not get driver"); return(null); } // Create fieldAttributes datasource DataSource ds = drv.CreateDataSource(@"C:\users\runarbe\desktop\test\test.gml", null); if (ds == null) { Debug.WriteLine("Could not get datasource"); return(null); } // Create fieldAttributes layer OSGeo.OGR.Layer l = ds.CreateLayer("AddressUnits", null, wkbGeometryType.wkbPoint, null); if (l == null) { Debug.WriteLine("Could not create layer"); return(null); } return(l); }
/************************************ 对比并筛选 ***************************************************/ /// <summary> /// 通过ID数组提取出所有要素,创建动作 /// </summary> /// <param name="dzLine"></param> /// <param name="pdLing"></param> /// <param name="savePath"></param> public static void selectFeat(string dzx, string pdx, string savePath) { Stopwatch ssww = new Stopwatch(); ssww.Start(); //获得数组 int[] a = getMinIdGroupM(dzx, pdx); Console.WriteLine("ID to Feater..."); OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzLayer = dzDS.GetLayerByIndex(0); if (System.IO.File.Exists(savePath)) { System.IO.File.Delete(savePath); } OSGeo.OGR.DataSource ds = dr.CreateDataSource(savePath, null); OSGeo.OGR.Layer fileLayer = ds.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); //get featuer by ID for (int i = 0; i < a.Length; i++) { fileLayer.CreateFeature(dzLayer.GetFeature(a[i])); dzLayer.GetFeature(a[i]).Dispose(); } dzLayer.Dispose(); dzDS.Dispose(); fileLayer.Dispose(); ds.Dispose(); _2_坡度线.OutLine.CutImg.cleanPdxM(savePath); ssww.Stop(); Console.WriteLine("提取轮廓线完成!用时:{0}", ssww.Elapsed.ToString()); }
public static void selectDZXFromPointM(string point, string dzx, string savePath) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource poiDS = dr.Open(point, 0); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); if (File.Exists(savePath)) { File.Delete(savePath); } OSGeo.OGR.DataSource newDS = dr.CreateDataSource(savePath, null); OSGeo.OGR.Layer newLayer = newDS.CreateLayer("", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); tickTime = 0; int poiCount = poiLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { subAdd(poiLayer.GetFeature(i), dzx, newLayer); } while (tickTime < poiCount) { Thread.Sleep(2222); Console.WriteLine("getFeatureByPoint:{0}/{1}", tickTime, poiCount); } Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0)); newDS.Dispose(); poiDS.Dispose(); }
/// <summary> /// 通过是否相交和面积差,获得最小面积差相交要素的ID数组--------多线OK /// </summary> /// <param name="dzx"></param> /// <param name="pdx"></param> /// <returns></returns> private static int[] getMinIdGroupM(string dzx, string pdx) { Console.WriteLine("起用多线程提取轮廓线ID...."); OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource pdDS = dr.Open(pdx, 0); OSGeo.OGR.Layer pdLayer = pdDS.GetLayerByIndex(0); int pdCount = pdLayer.GetFeatureCount(0); for (int pdi = 0; pdi < pdCount; pdi++) { OSGeo.OGR.Feature pdFeat = pdLayer.GetFeature(pdi); select(pdFeat, dzx); } while (tickTime < pdCount) { Console.WriteLine("第{0}个完成,共{1}个", tickTime, pdCount); Thread.Sleep(2000); } pdLayer.Dispose(); pdDS.Dispose(); Console.WriteLine("多线程提取ID完成....."); return(minIdG.ToArray()); }
public OGRDataset(OSGeo.OGR.Layer layer) { m_layer = layer; ogr_utils.map_fields(layer, out m_fieldMapping, out m_fields, out m_datasetType, out m_geometryType, out m_geometryFieldIndex, out m_oidFieldIndex, out m_spatialReference); }
/// <summary> /// Loads a Ogr datasource with the specified layer /// </summary> /// <param name="Filename">datasource</param> /// <param name="LayerNum">number of layer</param> public Ogr(string Filename, int LayerNum) { this.Filename = Filename; OSGeo.OGR.Ogr.RegisterAll(); _OgrDataSource = OSGeo.OGR.Ogr.Open(this.Filename, 0); _OgrLayer = _OgrDataSource.GetLayerByIndex(LayerNum); }
/// <summary> /// Loads a Ogr datasource with the specified layer /// </summary> /// <param name="Filename">datasource</param> /// <param name="LayerName">name of layer</param> public Ogr(string Filename, string LayerName) { this.Filename = Filename; OSGeo.OGR.Ogr.RegisterAll(); _OgrDataSource = OSGeo.OGR.Ogr.Open(this.Filename, 1); _OgrLayer = _OgrDataSource.GetLayerByName(LayerName); }
public OGRDataset(Layer layer) { OGRLayer = layer; ogr_utils.MapFields(layer, out m_fieldMapping, out m_fields, out m_datasetType, out m_geometryType, out m_geometryFieldIndex, out m_oidFieldIndex, out m_spatialReference); }
/************************************* 点角式 *****************************************/ /// <summary> /// 清理等后生成新的文件 /// /// </summary> public static void claenPoint(string filePath, string savefile, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); //进来的Layer OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 0); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); //出去的Layer if (System.IO.File.Exists(savefile)) { savefile.deleteFiles(); } OSGeo.OGR.DataSource targDs = dr.CreateDataSource(savefile, null); OSGeo.OGR.Layer targLayer = targDs.CreateLayer("targ", null, OSGeo.OGR.wkbGeometryType.wkbPolygon, null); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { ///进来的Featuer OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); ///把一个Featuer转为点数组 OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0); int pointCount = subGeom.GetPointCount(); Point[] aFeat = new Point[pointCount]; for (int c = 0; c < pointCount; c++) { aFeat[c].X = subGeom.GetX(c); aFeat[c].Y = subGeom.GetY(c); aFeat[c].Z = subGeom.GetZ(c); } ///调选点方法,得到一个新的Featuer Geometry newGeom = null; if (aFeat.Length > cishu * 3) { newGeom = JID(aFeat, jiaodu, cishu); } else { oriFeat.Dispose(); continue; } if (newGeom != null) { Feature temp = new Feature(new FeatureDefn("")); temp.SetGeometry(newGeom); targLayer.CreateFeature(temp); temp.Dispose(); } oriFeat.Dispose(); Console.WriteLine("已完成{0}/{1}", i, featCount); } oriDs.Dispose(); targDs.Dispose(); }
public static bool OverlayOperate(DotSpatial.Data.IFeatureSet baseFeatureSet, DotSpatial.Data.IFeatureSet overlayFeatureSet, ref OSGeo.OGR.Layer resultLayer, OverlayType type, Ogr.GDALProgressFuncDelegate callback) { OSGeo.OGR.Layer baseLayer = VectorConverter.DS2OrgLayer(baseFeatureSet); OSGeo.OGR.Layer overlayLayer = VectorConverter.DS2OrgLayer(overlayFeatureSet); bool result = OverlayOperate(baseLayer, overlayLayer, ref resultLayer, type, callback); return(result); }
public static bool OverlayOperate(string baseLayerPath, string overlayLayerPath, ref OSGeo.OGR.Layer resultLayer, OverlayType type, Ogr.GDALProgressFuncDelegate callback) { OSGeo.OGR.Layer baseLayer = VectorConverter.GetOgrLayer(baseLayerPath); OSGeo.OGR.Layer overlayLayer = VectorConverter.GetOgrLayer(overlayLayerPath); bool result = OverlayOperate(baseLayer, overlayLayer, ref resultLayer, type, callback); return(result); }
/// <summary> /// Loads a OgrFeatureProvider datasource with the specified layer /// </summary> /// <param name="path">datasource</param> /// <param name="layerName">name of layer</param> public OgrFeatureProvider(string path, string layerName) : this() { this.path = path; this.layerName = layerName; ogrDataSource = Ogr.Open(path, 1); ogrLayer = ogrDataSource.GetLayerByName(layerName); Name = layerName; _IsOpen = true; }
private Extent GetExtent(OSGeo.OGR.Layer ogrLayer) { var envelope = new Envelope(); ogrLayer.GetExtent(envelope, 0); return(new Extent { MaxX = envelope.MaxX, MaxY = envelope.MaxY, MinX = envelope.MinX, MinY = envelope.MinY }); }
/// <summary> /// Loads a OgrFeatureProvider datasource with the specified layer /// </summary> /// <param name="path">datasource</param> /// <param name="layerNumber">number of layer</param> public OgrFeatureProvider(string path, int layerNumber) : this() { this.path = path; ogrDataSource = Ogr.Open(path, 0); ogrLayer = ogrDataSource.GetLayerByIndex(layerNumber); this.layerName = ogrLayer.GetName(); Name = LayerName; _IsOpen = true; }
public static HandleRef getCPtrAndDisown(Layer obj, object parent) { if (obj != null) { obj.swigCMemOwn = false; obj.swigParentRef = parent; return obj.swigCPtr; } else { return new HandleRef(null, IntPtr.Zero); } }
public static FieldDefn[] Fields(this OSGeo.OGR.Layer layer) { var defn = layer.GetLayerDefn(); var count = defn.GetFieldCount(); List <FieldDefn> fields = new List <FieldDefn>(); for (int i = 0; i < count; i++) { var field = defn.GetFieldDefn(i); fields.Add(field); } return(fields.ToArray()); }
/// <summary> /// 子线程提取轮廓线ID /// </summary> /// <param name="pdFeat"></param> /// <param name="dzx"></param> async static void select(Feature pdFeat, string dzx) { await Task.Run(() => { double afterCha = -1; int yesID = -1; OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource dzDS = dr.Open(dzx, 0); OSGeo.OGR.Layer dzxLayer = dzDS.GetLayerByIndex(0); OSGeo.OGR.Geometry pdGeom = pdFeat.GetGeometryRef(); int dzCount = dzxLayer.GetFeatureCount(0); for (int dzi = 0; dzi < dzCount; dzi++) { OSGeo.OGR.Feature dzFeat = dzxLayer.GetFeature(dzi); OSGeo.OGR.Geometry dzGeom = dzFeat.GetGeometryRef(); if (pdGeom.Intersect(dzGeom)) { double cha = Math.Abs(dzGeom.GetArea() - pdGeom.GetArea()); if (afterCha == -1) { afterCha = cha; yesID = dzi; } else if (cha < afterCha) { afterCha = cha; yesID = dzi; } } dzGeom.Dispose(); dzFeat.Dispose(); } if (yesID != -1) { minIdG.Add(yesID); } dzDS.Dispose(); dzxLayer.Dispose(); pdGeom.Dispose(); pdFeat.Dispose(); tickTime++; }); }
public Layer(string name, DataSource ds) { this.ds = ds; this.name = name; Console.WriteLine(" Zak³adanie warstwy " + name); srs21.ImportFromProj4(proj21); layer = ds.CreateLayer(name, srs21, wkbGeometryType.wkbPolygon, new string[] { }); if (layer == null) { throw new Exception("-FAILED-"); } }
private string GetSpatialReference(OSGeo.OGR.Layer layer, int iLayer, DataSource dataSource, SpatialReference sourceSRS) { string spatialReference; if (layer.GetSpatialRef() == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is missing. SRS set automatically set to WGS84."); Driver driver = dataSource.GetDriver(); if (driver.GetName() == "MVT") { sourceSRS.SetFromUserInput("EPSG:3857"); } else { sourceSRS.SetFromUserInput("WGS84"); } ///this seems to work where SetWellKnownGeogCS doesn't string pretty; sourceSRS.ExportToPrettyWkt(out pretty, 0); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, pretty); return(spatialReference = "Spatial Reference System (SRS) is missing. SRS set automatically set to WGS84."); } else { if (layer.GetSpatialRef().Validate() != 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Spatial Reference System (SRS) is unknown or unsupported. SRS set automatically set to WGS84."); sourceSRS.SetWellKnownGeogCS("WGS84"); return(spatialReference = "Spatial Reference System (SRS) is unknown or unsupported. SRS set automatically set to WGS84."); } else { sourceSRS = layer.GetSpatialRef(); sourceSRS.ExportToWkt(out spatialReference); try { int sourceSRSInt = Int16.Parse(sourceSRS.GetAuthorityCode(null)); AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "The source Spatial Reference System (SRS) from layer " + layer.GetName() + " is EPSG:" + sourceSRSInt + "."); } catch { AddRuntimeMessage(GH_RuntimeMessageLevel.Remark, "Failed to get an EPSG Spatial Reference System (SRS) integer from layer " + layer.GetName() + "."); } } } return(spatialReference); }
public static void selectFromPoint(string point, string slopPoly) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource poiDS = dr.Open(point, 0); OSGeo.OGR.DataSource slopDS = dr.Open(slopPoly, 1); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); OSGeo.OGR.Layer slopLayer = slopDS.GetLayerByIndex(0); List <int> staIds = new List <int>(); int poiCount = WorkFlow.pointIds.Count; int slopCount = slopLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { OSGeo.OGR.Feature poiFeat = poiLayer.GetFeature(WorkFlow.pointIds[i]); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int t = 0; t < slopCount; t++) { OSGeo.OGR.Feature slopFeat = slopLayer.GetFeature(t); OSGeo.OGR.Geometry slopGeom = slopFeat.GetGeometryRef(); if (poiGeom.Within(slopGeom)) { staIds.Add(t); WorkFlow.pointIds[i] = -1; } slopFeat.Dispose(); } poiFeat.Dispose(); } Console.WriteLine("【本次提取到{0}个要素】", staIds.Count); while (WorkFlow.pointIds.IndexOf(-1) > -1) { WorkFlow.pointIds.Remove(-1); } for (int i = 0; i < slopCount; i++) { if (staIds.IndexOf(i) == -1) { slopLayer.DeleteFeature(i); } } slopDS.deleteFeatUpdate(); slopDS.Dispose(); poiDS.Dispose(); }
/// <summary> /// 清理原文件 /// </summary> /// <param name="filePath"></param> public static void claenPoint(string filePath, double jiaodu, int cishu) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource oriDs = dr.Open(filePath, 1); OSGeo.OGR.Layer oriLayer = oriDs.GetLayerByIndex(0); int featCount = oriLayer.GetFeatureCount(0); for (int i = 0; i < featCount; i++) { OSGeo.OGR.Feature oriFeat = oriLayer.GetFeature(i); OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef(); OSGeo.OGR.Geometry subGeom = oriGeom.GetGeometryRef(0); int pointCount = subGeom.GetPointCount(); Point[] aFeat = new Point[pointCount]; for (int c = 0; c < pointCount; c++) { aFeat[c].X = subGeom.GetX(c); aFeat[c].Y = subGeom.GetY(c); aFeat[c].Z = subGeom.GetZ(c); } OSGeo.OGR.Geometry newGeom = null; if (aFeat.Length > cishu * 3) { newGeom = JID(aFeat, jiaodu, cishu); } else { oriFeat.Dispose(); continue; } if (newGeom != null) { oriFeat.SetGeometry(newGeom); oriLayer.SetFeature(oriFeat); } Console.WriteLine("cleanPoint已完成{0}/{1}", i, featCount); oriFeat.Dispose(); } oriDs.Dispose(); }
/// <summary> /// Overlay options /// </summary> /// <param name="baseLayerPath">the complete path of the base layer</param> /// <param name="overlayLayerPath">the complete path of the overlay layer</param> /// <param name="resultLayerPath">the complete path of result layer</param> /// <param name="type">overlay option type</param> public static bool OverlayOperate(OSGeo.OGR.Layer baseLayer, OSGeo.OGR.Layer overlayLayer, ref OSGeo.OGR.Layer resultLayer, OverlayType type, Ogr.GDALProgressFuncDelegate callback) { try { string[] options = new string[] { "SKIP_FAILURES=YES" }; switch (type) { case OverlayType.Intersects: baseLayer.Intersection(overlayLayer, resultLayer, options, callback, "Intersection"); break; case OverlayType.Union: baseLayer.Union(overlayLayer, resultLayer, options, callback, "Union"); break; case OverlayType.SymDifference: baseLayer.SymDifference(overlayLayer, resultLayer, options, callback, "SymDifference"); break; case OverlayType.Identity: baseLayer.Identity(overlayLayer, resultLayer, options, callback, "Identity"); break; case OverlayType.Update: baseLayer.Update(overlayLayer, resultLayer, options, callback, "Update"); break; case OverlayType.Clip: baseLayer.Clip(overlayLayer, resultLayer, options, callback, "Clip"); break; case OverlayType.Erase: baseLayer.Erase(overlayLayer, resultLayer, options, callback, "Erase"); break; } return(true); } catch (Exception e) { //throw e; return(false); } }
public static void ConvertSHPtoGJSON(string shapefilePath, out StringBuilder gJSON) { string shpPath = shapefilePath; Ogr.RegisterAll(); OSGeo.OGR.Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); var ds = drv.Open(shpPath, 0); /* * Driver geoJSONDriver = Ogr.GetDriverByName("GeoJSON"); * * string geojsonfilepath = @"c:\temp\us_counties_test.json"; * * if (System.IO.File.Exists(geojsonfilepath)) * System.IO.File.Delete(geojsonfilepath); * * geoJSONDriver.CreateDataSource(@"c:\temp\us_counties_test.json", null); */ OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.Feature f; layer.ResetReading(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); var options = new[] { "-f", "GeoJSON", "-lco", "RFC7946=YES" }; while ((f = layer.GetNextFeature()) != null) { var geom = f.GetGeometryRef(); if (geom != null) { var geometryJson = geom.ExportToJson(options); sb.AppendLine(geometryJson); } } gJSON = sb; }
public OgrDataReader(string sDataSource) { //wkbReader = new DotSpatial.Data.WKBReader(); _ogrDataSource = Ogr.Open(sDataSource, 0); _ogrLayer = _ogrDataSource.GetLayerByIndex(0); _iFeatureCount = _ogrLayer.GetFeatureCount(1); _ogrFeatureDefinition = _ogrLayer.GetLayerDefn(); _iFieldCount = _ogrFeatureDefinition.GetFieldCount(); BuildSchemaTable(); _currentFeature = null; bClosed = false; }
public IPlugInDatasetHelper OpenDataset(string localName) { if (m_connString == null) { return(null); } OSGeo.OGR.Layer layer = m_datasource.GetLayerByName(localName); if (layer == null) { return(null); } OGRDataset ds = new OGRDataset(layer); return((IPlugInDatasetHelper)ds); }
public static string addPdx(string point, string inDSM, int lev) { //读取点ID OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource pointDs = dr.Open(point, 0); OSGeo.OGR.Layer pointLayer = pointDs.GetLayerByIndex(0); int pointCount = pointLayer.GetFeatureCount(0); for (int i = 0; i < pointCount; i++) { pointIds.Add(i); } //提升分辨率,计算坡度图 List <string> subSlop = new List <string>(); string upDataDSM = StaticTools.tempFilePath("img", "UpDataDSM"); CutImg.ImgReProject(inDSM, upDataDSM, 2); string addSolpeMap = StaticTools.tempFilePath("img", "addSolpeMap"); SlopeDem.Instance.Sloping(upDataDSM, addSolpeMap); //循环坡度下限值 for (int i = 0; i < lev; i++) { //没点了就退出 if (pointIds.Count == 0) { Console.WriteLine("所有目标都已找到匹配元素!"); break; } Console.WriteLine("【开始第{0}次循环,还有{1}个点待提取!】", i, pointIds.Count); string _outShpPath = StaticTools.tempFilePath("shp", "SlopAdd" + i.ToString() + "Times"); GetOutlines.Instance.MyGetOutlines(upDataDSM, addSolpeMap, _outShpPath, 80 - i * 5); 补充要素.AddFeatuer.selectFromPoint(point, _outShpPath); subSlop.Add(_outShpPath); } Console.WriteLine("还有{0}个目标没有找到对应要素!", pointIds.Count.ToString()); return(OutLine.CutImg.zhengHePdx(subSlop, 10)); }
/// <summary> /// Adds the projection info of the OGR layer to the FeatureSet. /// </summary> /// <param name="layer">The layer that is used to create the FeatureSet.</param> /// <returns>Null on error otherwise the ProjectionInfo object that contains the data of the layers proj4 string.</returns> private static ProjectionInfo GetProjectionInfo(Layer layer) { try { SpatialReference osrSpatialref = layer.GetSpatialRef(); if (osrSpatialref != null) { string sProj4String; osrSpatialref.ExportToProj4(out sProj4String); return(ProjectionInfo.FromProj4String(sProj4String)); } } catch (Exception ex) { Trace.WriteLine(ex); } return(null); }
public static void selectDZXFromPoint(string point, string dzx, string savePath) { OSGeo.OGR.Ogr.RegisterAll(); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); OSGeo.OGR.DataSource poiDS = dr.Open(point, 0); OSGeo.OGR.DataSource dzxDS = dr.Open(dzx, 0); if (File.Exists(savePath)) { File.Delete(savePath); } OSGeo.OGR.DataSource newDS = dr.CreateDataSource(savePath, null); OSGeo.OGR.Layer poiLayer = poiDS.GetLayerByIndex(0); OSGeo.OGR.Layer dzxLayer = dzxDS.GetLayerByIndex(0); OSGeo.OGR.Layer newLayer = newDS.CreateLayer("", dzxLayer.GetSpatialRef(), dzxLayer.GetGeomType(), null); int poiCount = poiLayer.GetFeatureCount(0); int dzxCount = dzxLayer.GetFeatureCount(0); for (int i = 0; i < poiCount; i++) { OSGeo.OGR.Feature poiFeat = poiLayer.GetFeature(i); OSGeo.OGR.Geometry poiGeom = poiFeat.GetGeometryRef(); for (int t = 0; t < dzxCount; t++) { OSGeo.OGR.Feature dzxFeat = dzxLayer.GetFeature(t); OSGeo.OGR.Geometry dzxGeom = dzxFeat.GetGeometryRef(); if (poiGeom.Within(dzxGeom)) { newLayer.CreateFeature(dzxFeat); } dzxFeat.Dispose(); Console.WriteLine("getFeatureByPoint:{0}/{1}", i, poiCount); } poiFeat.Dispose(); } Console.WriteLine("【本次提取到{0}个要素】", newLayer.GetFeatureCount(0)); newDS.Dispose(); dzxDS.Dispose(); poiDS.Dispose(); }
private List <Layer> GetLayers(DataSource dataSource) { List <OSGeo.OGR.Layer> layerSet = new List <OSGeo.OGR.Layer>(); for (int layerIndex = 0; layerIndex < dataSource.GetLayerCount(); layerIndex++) { OSGeo.OGR.Layer layer = dataSource.GetLayerByIndex(layerIndex); if (layer == null) { Console.WriteLine("Couldn't fetch advertised layer " + layerIndex); System.Environment.Exit(-1); } else { layerSet.Add(layer); } } return(layerSet); }
/// <summary> /// Closes the datasource /// </summary> public virtual void Close() { //make sure to release unmanaged objects if (ogrDataSource != null) { ogrDataSource.Dispose(); ogrDataSource = null; } if (ogrLayer != null) { ogrLayer.Dispose(); ogrLayer = null; } _IsOpen = false; }
public void ReleaseResultSet(Layer layer) { OgrPINVOKE.DataSource_ReleaseResultSet(swigCPtr, Layer.getCPtrAndDisown(layer, ThisOwn_false())); if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve(); }
public Layer CopyLayer(Layer src_layer, string new_name, string[] options) { IntPtr cPtr = OgrPINVOKE.DataSource_CopyLayer(swigCPtr, Layer.getCPtr(src_layer), new_name, (options != null)? new OgrPINVOKE.StringListMarshal(options)._ar : null); Layer ret = (cPtr == IntPtr.Zero) ? null : new Layer(cPtr, false, ThisOwn_false()); if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve(); return ret; }
public int Clip(Layer method_layer, Layer result_layer, string[] options, Ogr.GDALProgressFuncDelegate callback, string callback_data) { int ret = OgrPINVOKE.Layer_Clip(swigCPtr, Layer.getCPtr(method_layer), Layer.getCPtr(result_layer), (options != null)? new OgrPINVOKE.StringListMarshal(options)._ar : null, callback, callback_data); if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve(); return ret; }
public static void ConvertFeatureToRaster(Layer layer, out Dataset outputDataset, double rasterCellSize, string fieldName) { DriverUtils.RegisterGdalOgrDrivers(); Envelope envelope = new Envelope(); layer.GetExtent(envelope, 0); //Compute the out raster cell resolutions int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize); int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize); //Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY); //Console.WriteLine("X resolution: " + x_res); //Console.WriteLine("X resolution: " + y_res); //Create new tiff in disk string tempRaster = "tempZoneRaster.tif"; if (File.Exists(tempRaster)) { File.Delete(tempRaster); } OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff"); outputDataset = outputDriver.Create(tempRaster, x_res, y_res, 1, DataType.GDT_Int32, null); //Extrac srs from input feature string inputShapeSrs; SpatialReference spatialRefrence = layer.GetSpatialRef(); spatialRefrence.ExportToWkt(out inputShapeSrs); //Assign input feature srs to outpur raster outputDataset.SetProjection(inputShapeSrs); //Geotransform double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize }; outputDataset.SetGeoTransform(argin); //Set no data Band band = outputDataset.GetRasterBand(1); //band.SetNoDataValue(GdalUtilConstants.NoDataValue); band.Fill(GdalUtilConstants.NoDataValue, 0.0); //Feature to raster rasterize layer options //No of bands (1) int[] bandlist = new int[] { 1 }; //Values to be burn on raster (10.0) double[] burnValues = new double[] { 10.0 }; //Dataset myDataset = Gdal.Open(outputRasterFile, Access.GA_Update); //additional options string[] rasterizeOptions; //rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName }; //Rasterize layer //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes //Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion"); Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, null, null); }
public virtual void OpenLayerWithSQL(string statement) { layerName = null; try { ogrLayer = ogrDataSource.ExecuteSQL(statement, null, null); } catch { } }
public static HandleRef getCPtr(Layer obj) { return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; }
private void OpenLayer() { ogrLayer = ogrDataSource.GetLayerByName(LayerName); }