/// <summary> /// 存储shp对应的featuresource,可以将postgis原生featuresource转存为shapefile /// </summary> /// <param name="tablename"></param> /// <returns></returns> public Boolean SaveSHPFeatureSource(string tablename, string path, string filename) { if (this.featureSources.ContainsKey(tablename)) { return(SHPGeoDataBase.SaveFeatureSource2File(this.featureSources[tablename], path, filename)); } return(false); }
//读取一个shapefile文件并将其中的第一个图层添加到当前SHPGeoDataBase public override String AddFeatureSource(String path) { FeatureSource tmpSrc = SHPGeoDataBase.GetFeaturesource(this, path); if (this.pathRef.ContainsKey(tmpSrc.schema.name)) { return(null); } this.pathRef.Add(tmpSrc.schema.name, path); this.featureSources.Add(tmpSrc.schema.name, tmpSrc); return(tmpSrc.schema.name); }
//给定路径,创建SHPGeoDataBase实例,并从数据库中读取所有图层的schema,必须不包含同名表,否则返回null public static SHPGeoDataBase GetGeoDataBase(List <String> pathList) { SHPGeoDataBase resultDB = new SHPGeoDataBase(); foreach (String path in pathList) { FeatureSource tmpSrc = SHPGeoDataBase.GetFeaturesource(resultDB, path); if (resultDB.pathRef.ContainsKey(tmpSrc.schema.name)) { return(null); } resultDB.pathRef.Add(tmpSrc.schema.name, path); resultDB.featureSources.Add(tmpSrc.schema.name, tmpSrc); } return(resultDB); }
//get a ogr datasorce by path string private static OSGeo.OGR.DataSource GetOGRDataSource(SHPGeoDataBase thisdb, String path) { OSGeo.OGR.Ogr.RegisterAll(); //to support chinese path OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //to support chinese field name OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile"); if (dr == null) { return(null); } //第二参数为0表明只读模式 OSGeo.OGR.DataSource ds = dr.Open(path, 0); thisdb.iDses.Add(path, ds); return(ds); }
/// <summary> /// 给定shapefile的path,读取数据featuresource并加入到postgisgeodatabase中,返回该featuresource的表名字 /// </summary> /// <param name="path"></param> /// <returns></returns> public string AddSHPFeatureSource(string path, string guid) { if (shpgeodb == null) { shpgeodb = SHPGeoDataBase.GetGeoDataBase(new List <string>()); } string tablename = shpgeodb.AddFeatureSource(path); if (this.featureSources.ContainsKey(tablename)) { return(null); } var source = shpgeodb.featureSources[tablename]; source.schema.name = guid; this.featureSources.Add(guid, source); this.pathRef.Add(guid, guid); return(tablename); }
//从文件中读取数据 private static FeatureSource GetFeaturesource(SHPGeoDataBase thisdb, String path) { OSGeo.OGR.DataSource ds = GetOGRDataSource(thisdb, path); //init schema OSGeo.OGR.Layer layer = ds.GetLayerByIndex(0); OSGeo.OGR.FeatureDefn fd = layer.GetLayerDefn(); Int32 sIndex = path.LastIndexOf("\\"); string sname = path.Substring(sIndex + 1, path.Length - sIndex - 1 - 4); int fieldcount = fd.GetFieldCount(); Dictionary <String, FieldDefn> tmpField = new Dictionary <string, FieldDefn>(); for (int i = 0; i < fieldcount; ++i) { OSGeo.OGR.FieldDefn field = fd.GetFieldDefn(i); tmpField.Add(field.GetName(), field); } GisSmartTools.RS.ReferenceSystem rfs; OSGeo.OSR.SpatialReference osrrf = layer.GetSpatialRef(); if (osrrf.IsProjected() != 0) { rfs = new GisSmartTools.RS.SRS(osrrf); } else { rfs = new GisSmartTools.RS.GRS(osrrf); } Schema rs = new Schema(sname, layer.GetGeomType(), rfs, tmpField); //get featurecollection FeatureCollection fc = GetFeatureCollection(layer, rs); //close file //ds.Dispose(); return(new FeatureSource(rs, fc)); }