public FeatureDataTable GetFeatureTableFromShapefile(string shapeFilePath) { GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices(); int cd; if (!int.TryParse(_enconding.Text, out cd)) { _status.Text = "Bad code page"; } ShapeFile sf = new ShapeFile(shapeFilePath); sf.Encoding = Encoding.GetEncoding(int.Parse(_enconding.Text)); sf.Open(); this.shapeSRID = sf.SRID; this.shapeExtends = sf.GetExtents(); _sRID.Text = this.shapeSRID.ToString(); FeatureDataSet ds = new FeatureDataSet(); sf.ExecuteIntersectionQuery(this.shapeExtends, ds); //ds.Tables[0].Columns.Remove("Oid"); return(ds.Tables[0]); }
/// <summary> /// This method returns a FeatureDataTable containing all the rows from the shapefile that intersect the testGeometry. /// The ShapeFile.ExecuteIntersectionQuery method only tests bounding boxes so we use the FilterDelegate property to add a true /// intersection test using NetTopologySuite /// </summary> /// <param name="pathToShapefile">The path to the shapefile</param> /// <param name="testGeometry">The geometry that we want to test against</param> /// <returns></returns> public FeatureDataTable GetIntersectingFeaturesUsingFilterDelegate(string pathToShapefile, SMGeometry testGeometry) { //create a new shapefile provider using (ShapeFile shapefile = new ShapeFile(pathToShapefile)) { //create an nts GeometryFactory GeometryFactory geometryFactory = new GeometryFactory(); //convert the testGeometry into the equivalent NTS geometry GeoAPI.Geometries.IGeometry testGeometryAsNtsGeom = GeometryConverter.ToNTSGeometry(testGeometry, geometryFactory); SMGeometry check = GeometryConverter.ToSharpMapGeometry(testGeometryAsNtsGeom); if (!check.Equals(testGeometry)) { throw new ApplicationException("conversion error"); } //set the shapefile providers' FilterDelegate property to a new anonymous method //this delegate method will be called for each potential row shapefile.FilterDelegate = delegate(FeatureDataRow featureDataRow) { //get the geometry from the featureDataRow SMGeometry rowGeometry = featureDataRow.Geometry; //convert it to the equivalent NTS geometry GeoAPI.Geometries.IGeometry compareGeometryAsNtsGeometry = GeometryConverter.ToNTSGeometry(rowGeometry, geometryFactory); //do the test. Note that the testGeometryAsNtsGeometry is available here because it is //declared in the same scope as the anonymous method. bool intersects = testGeometryAsNtsGeom.Intersects(compareGeometryAsNtsGeometry); //return the result return(intersects); }; //create a new FeatureDataSet FeatureDataSet featureDataSet = new FeatureDataSet(); //open the shapefile shapefile.Open(); //call ExecuteIntersectionQuery. The FilterDelegate will be used to limit the result set shapefile.ExecuteIntersectionQuery(testGeometry, featureDataSet); //close the shapefile shapefile.Close(); //return the populated FeatureDataTable return(featureDataSet.Tables[0]); } }
private GeographicSpeciesListDto ReadSpeciesDistributionFromShapeFiles(ShapeFile shapeFile, GeographyPoint coordinates) { var speciesInfoDictionary = new Dictionary <String, GeographicSpeciesInfoDto>(); var geometryObject = Geometry.DefaultFactory.CreatePoint(new Coordinate(coordinates.Longitude, coordinates.Latitude)); var ds = new FeatureDataSet(); //example uses a map image, but this could be a layer generated with code shapeFile.ExecuteIntersectionQuery(geometryObject.Centroid, ds); foreach (var dt in ds.Tables) { foreach (DataRow row in dt.Rows) { var speciesInfoDto = new GeographicSpeciesInfoDto { Kingdom = row.ItemArray[17].ToString(), Phylum = row.ItemArray[18].ToString(), Class = row.ItemArray[19].ToString(), Order = row.ItemArray[20].ToString(), Family = row.ItemArray[21].ToString(), Genus = row.ItemArray[22].ToString(), Species = row.ItemArray[23].ToString(), PresenceCode = Int32.Parse(row.ItemArray[14].ToString()) }; var key = speciesInfoDto.Species.Trim().ToLower(); //Presence code of >= 4 means the species probably doesn't exist in the area ("Possibly extinct") if (!speciesInfoDictionary.ContainsKey(key) && speciesInfoDto.PresenceCode < 4) { speciesInfoDictionary.Add(key, speciesInfoDto); } } } var geographicSpeciesListDto = new GeographicSpeciesListDto { Latitude = coordinates.Latitude, Longitude = coordinates.Longitude, SpeciesInfoDtoList = speciesInfoDictionary.Values.ToList() }; return(geographicSpeciesListDto); }
public override void Render(IGraphics g, Map map) { try { _shapeFile.Open(); var ds = new FeatureDataSet(); _shapeFile.ExecuteIntersectionQuery(map.Envelope, ds); var dt = ds.Tables[0]; foreach (FeatureDataRow fdr in dt.Rows) { if (fdr.Geometry.EnvelopeInternal.Intersects(map.Envelope)) { var file = fdr[_fieldName] as string; if (!Path.IsPathRooted(file)) { file = Path.Combine(Path.GetDirectoryName(_fileName), file); } if (_logger.IsDebugEnabled) { _logger.Debug("Drawing " + file); } OpenDataset(file); base.Render(g, map); _envelope = null; if (_gdalDataset != null) { _gdalDataset.Dispose(); _gdalDataset = null; } } } } catch (Exception) { _shapeFile.Close(); } }
public override void Render(Graphics g, IMapViewPort map) { try { _shapeFile.Open(); var ds = new FeatureDataSet(); _shapeFile.ExecuteIntersectionQuery(map.Envelope, ds); var dt = ds.Tables[0]; foreach (FeatureDataRow fdr in dt.Rows) { if (fdr.Geometry.EnvelopeInternal.Intersects(map.Envelope)) { var file = fdr[_fieldName] as string; if (!Path.IsPathRooted(file)) { file = Path.Combine(Path.GetDirectoryName(_fileName), file); } if (file == null) { continue; } if (_logger.IsDebugEnabled) { _logger.Debug("Drawing " + file); } if (!_openDatasets.ContainsKey(file)) { OpenDataset(file); _openDatasets.Add(file, new CacheHolder() { Bands = Bands, Dataset = _gdalDataset, Envelope = _envelope, HistoBounds = HistoBounds, ImageSize = _imageSize, Projection = Projection }); } else { CacheHolder hld = _openDatasets[file]; Bands = hld.Bands; _gdalDataset = hld.Dataset; _envelope = hld.Envelope; HistoBounds = hld.HistoBounds; _imageSize = hld.ImageSize; Projection = hld.Projection; } base.Render(g, map); _envelope = null; _gdalDataset = null; } } } catch (Exception) { _shapeFile.Close(); } }
public JsonResult GetData(float w, float n, float e, float s, int z) { string format = String.Format("~/App_Data/berlin/{0}", "osmbuildings.shp"); string path = this.HttpContext.Server.MapPath(format); if (!System.IO.File.Exists(path)) { throw new FileNotFoundException("file not found", path); } Point start = this.GeoToPixel(n, w, z); var meta = new { n, w, s, e, x = start.X, y = start.Y, z }; Envelope bbox = new Envelope(); bbox.ExpandToInclude(new Coordinate(n, w)); bbox.ExpandToInclude(new Coordinate(s, e)); FeatureDataSet ds = new FeatureDataSet(); using (ShapeFile provider = new ShapeFile(path)) { provider.DoTrueIntersectionQuery = true; provider.Open(); provider.ExecuteIntersectionQuery(bbox, ds); provider.Close(); } int zz = MaxZoom - z; List <object> data = new List <object>(); FeatureDataTable table = ds.Tables[0]; foreach (FeatureDataRow row in table) { int c = (short)(row["height"]); if (c == 0) { c = 5; // default value for "null" (zero) heights } int h = c * ScaleZ >> zz; if (h <= 1) { h = 1; } IGeometry geometry = row.Geometry; Coordinate[] coords = geometry.Coordinates; int total = coords.Length; double[] values = new double[total * 2]; int i = 0; foreach (Coordinate curr in coords) { Point p = this.GeoToPixel(curr.X, curr.Y, z); values[i++] = p.X - start.X; values[i++] = p.Y - start.Y; } data.Add(new object[] { h, values }); } return(this.Json(new { meta, data }, JsonRequestBehavior.AllowGet)); }
public static int ReadShapefile(string file, out List <Geometry> features, out List <double> intervals, out List <int> types, out List <double> heights) { int flag = 0; features = new List <Geometry>(); types = new List <int>(); heights = new List <double>(); intervals = new List <double>(); ShapeFile shp = null; try { shp = new ShapeFile(file); } catch (Exception e) { MainConsole.Instance.Output("Shapefile open failed!"); throw new Exception("ShapeFile not exists! " + e.Message + e.StackTrace); } shp.Open(); switch (shp.ShapeType) { case ShapeType.Point: flag = 0; break; case ShapeType.PolyLine: flag = 1; break; case ShapeType.Polygon: flag = 2; break; default: flag = -1; return(flag); } FeatureDataSet ds = new FeatureDataSet(); BoundingBox bbox = shp.GetExtents(); shp.ExecuteIntersectionQuery(bbox, ds); FeatureDataTable table = ds.Tables[0] as FeatureDataTable; try { foreach (FeatureDataRow row in table.Rows) { features.Add(row.Geometry); heights.Add(Convert.ToDouble(row["Height"])); types.Add(Convert.ToInt32(row["Type"])); if (flag != 0) { intervals.Add(Convert.ToDouble(row["Interval"])); } } } catch (Exception e) { throw new Exception("Attribute in shapefile incorrect with " + e.Message + e.StackTrace); } shp.Close(); return(flag); }