//将内存中一个图层的数据另存为在文件中 private Boolean SaveAsToFile(String name, String path) { Ogr.RegisterAll(); //to support chinese path OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); //to support chinese field name OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); string strDriverName = "ESRI Shapefile"; Driver oDriver = Ogr.GetDriverByName(strDriverName); if (oDriver == null) { //说明driver不可用 return(false); } String pathPrefix = System.IO.Path.GetDirectoryName(path); String pathPostfix = System.Guid.NewGuid().ToString(); if (File.Exists(pathPrefix + "\\" + pathPostfix)) { File.Delete(pathPrefix + "\\" + pathPostfix); } //create a datasource, if successfully, a shp file would be created DataSource oDs = oDriver.CreateDataSource(pathPrefix + "\\" + pathPostfix, null); Schema tmpSchema = this.featureSources[name].schema; Layer layer = oDs.CreateLayer(name, tmpSchema.rs.spetialReference, tmpSchema.geometryType, null); //insert all the fields to layer //use index instead of field name to avoid the error caused by chinese words int fieldcount = tmpSchema.fields.Count; for (int i = 0; i < fieldcount; ++i) { layer.CreateField(tmpSchema.fields.ElementAt(i).Value, 1); } FeatureDefn fdef = layer.GetLayerDefn(); FeatureCollection fc = this.featureSources[name].features; int fcount = fc.count; for (int i = 0; i < fcount; ++i) { GisSmartTools.Data.Feature tmpFeature = fc.featureList[i]; if (!tmpFeature.visible) { continue; } //create a new feature OSGeo.OGR.Feature newFeature = new OSGeo.OGR.Feature(fdef); //set attribute newFeature.SetFID(tmpFeature.featureID); for (int j = 0; j < fieldcount; ++j) { String fieldname = tmpSchema.fields.ElementAt(j).Key; FieldType ft = tmpSchema.fields[fieldname].GetFieldType(); try{ switch (ft) { case FieldType.OFTString: newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]); break; case FieldType.OFTInteger: newFeature.SetField(j, (int)tmpFeature.attributes[fieldname]); break; case FieldType.OFTReal: newFeature.SetField(j, (double)tmpFeature.attributes[fieldname]); break; case FieldType.OFTWideString: newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]); break; default: newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]); break; } }catch (Exception e) { } } //get geometry OSGeo.OGR.wkbGeometryType featType = tmpFeature.geometry.geometryType; OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(featType); if (geo != null) { switch (featType) { case OSGeo.OGR.wkbGeometryType.wkbPoint: GisSmartTools.Geometry.PointD tmpPoint = (GisSmartTools.Geometry.PointD)tmpFeature.geometry; geo.AddPoint(tmpPoint.X, tmpPoint.Y, 0.00); break; case OSGeo.OGR.wkbGeometryType.wkbLineString: GisSmartTools.Geometry.SimplePolyline tmpLine = (GisSmartTools.Geometry.SimplePolyline)tmpFeature.geometry; foreach (GisSmartTools.Geometry.PointD po in tmpLine.points) { geo.AddPoint(po.X, po.Y, 0.00); } break; case wkbGeometryType.wkbMultiLineString: GisSmartTools.Geometry.Polyline lines = (GisSmartTools.Geometry.Polyline)tmpFeature.geometry; foreach (SimplePolyline line in lines.childPolylines) { OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLineString); foreach (GisSmartTools.Geometry.PointD point in line.points) { tmpgeo.AddPoint(point.X, point.Y, 0.00); } geo.AddGeometryDirectly(tmpgeo); } break; case OSGeo.OGR.wkbGeometryType.wkbPolygon: GisSmartTools.Geometry.SimplePolygon gon = (GisSmartTools.Geometry.SimplePolygon)tmpFeature.geometry; foreach (SimplePolyline ring7 in gon.rings) { OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); foreach (GisSmartTools.Geometry.PointD point in ring7.points) { tmpgeo.AddPoint(point.X, point.Y, 0.00); } geo.AddGeometryDirectly(tmpgeo); } break; case wkbGeometryType.wkbMultiPolygon: GisSmartTools.Geometry.Polygon gons = (GisSmartTools.Geometry.Polygon)tmpFeature.geometry; foreach (GisSmartTools.Geometry.SimplePolygon cgon in gons.childPolygons) { OSGeo.OGR.Geometry geogon = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon); foreach (GisSmartTools.Geometry.SimplePolyline ring6 in cgon.rings) { OSGeo.OGR.Geometry geoline = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing); foreach (GisSmartTools.Geometry.PointD point6 in ring6.points) { geoline.AddPoint(point6.X, point6.Y, 0.00); } geogon.AddGeometryDirectly(geoline); } geo.AddGeometryDirectly(geogon); } break; default: break; } } //set feature newFeature.SetGeometry(geo); //add to layer layer.CreateFeature(newFeature); } //call Dispose method to save to file oDs.Dispose(); DataSource tds; if (this.iDses.TryGetValue(path, out tds)) { tds.Dispose(); this.iDses.Remove(path); } //enumerate all the files in the temp file directory and move them to the output directory string[] tmpName = System.IO.Directory.GetFiles(pathPrefix + "\\" + pathPostfix); foreach (string file in tmpName) { string decFile = pathPrefix + "\\" + Path.GetFileName(file); if (File.Exists(decFile)) { File.Delete(decFile); } File.Move(file, decFile); } Directory.Delete(pathPrefix + "\\" + pathPostfix, true); return(true); }
public Circle(double x, double y, double r) { this.geometryType = OSGeo.OGR.wkbGeometryType.wkbUnknown; this.center = new PointD(x, y); this.radius = r; }