/// <summary> /// Reads a shapefile into a arraylist of features that need converting from x,y coordinates to Long and Lat coordinates /// </summary> /// <param name="filename">name of the shapefile (the file that has all the polygons for the footpaths)</param> /// <param name="fact">the class that generates the structure of the points</param> /// <returns></returns> public ArrayList ReadSHP(string filename, GeometryFactory fact) { ArrayList features = new ArrayList(); //Array list for all the coordinates from the shapefile ShapefileDataReader sfDataReader = new ShapefileDataReader(filename, fact); //takes a file and a factory to build the geometries ShapefileHeader shpHeader = sfDataReader.ShapeHeader; //reads the headers of the file for checking and looping purposes DbaseFileHeader DHeader = sfDataReader.DbaseHeader; while (sfDataReader.Read() == true) //reading through all the data in the shapefile { Feature feature = new Feature(); //setting up a feature for each set of points AttributesTable atTable = new AttributesTable(); //table for the set of points string[] keys = new string[DHeader.NumFields]; Geometry geometry = sfDataReader.Geometry; for (int i = 0; i < DHeader.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = DHeader.Fields[i]; keys[i] = fldDescriptor.Name; atTable.Add(fldDescriptor.Name, sfDataReader.GetValue(i)); } feature.Geometry = geometry; feature.Attributes = atTable; //setting the variables for the feature features.Add(feature); } sfDataReader.Close();//closing the reader sfDataReader.Dispose(); return(features); }
private double GetRandomVectorValue(string file, int requests) { var featureIds = new int[requests]; var dataReader = new ShapefileDataReader(file, new GeometryFactory()); var numberOfFeatures = dataReader.RecordCount; Parallel.For(0, requests, index => { featureIds[index] = _rnd.Next(numberOfFeatures); }); // Close the file and reopen after messurement has started dataReader.Close(); dataReader.Dispose(); var reader = new ShapefileReader(file); var watch = System.Diagnostics.Stopwatch.StartNew(); var allFeatures = reader.ReadAll(); Parallel.ForEach(featureIds, id => { var feature = allFeatures[id]; // var result = feature; // Console.WriteLine(result); }); return(watch.Elapsed.Milliseconds); }
/// <summary> /// Reads a shapefile into a arraylist of features that need converting from x,y coordinates to Long and Lat coordinates /// </summary> /// <param name="filename">name of the shapefile (the file that has all the polygons for the footpaths)</param> /// <param name="fact">the class that generates the structure of the points</param> /// <returns></returns> public ArrayList ReadSHP(string filename, GeometryFactory fact) { ArrayList features = new ArrayList(); //Array list for all the coordinates from the shapefile ShapefileDataReader sfDataReader = new ShapefileDataReader(filename, fact); ShapefileHeader shpHeader = sfDataReader.ShapeHeader; DbaseFileHeader DHeader = sfDataReader.DbaseHeader; while (sfDataReader.Read() == true) { Feature feature = new Feature(); AttributesTable atTable = new AttributesTable(); string[] keys = new string[DHeader.NumFields]; Geometry geometry = sfDataReader.Geometry; for (int i = 0; i < DHeader.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = DHeader.Fields[i]; keys[i] = fldDescriptor.Name; atTable.Add(fldDescriptor.Name, sfDataReader.GetValue(i)); } feature.Geometry = geometry; feature.Attributes = atTable; features.Add(feature); } sfDataReader.Close(); sfDataReader.Dispose(); return(features); }
public void LoadCountries(string path) { iso2Countries = new Dictionary <string, Feature>(); iso3Countries = new Dictionary <string, Feature>(); countryFeatures = new ArrayList(); countriesDt = new System.Data.DataTable(); GeometryFactory factory = new GeometryFactory(); using (ShapefileDataReader reader = new ShapefileDataReader(path, factory)) { foreach (var f in reader.DbaseHeader.Fields) { countriesDt.Columns.Add(f.Name, f.Type); } var keyCol = countryDtKeyCandidates .Intersect(countriesDt.Columns.OfType <System.Data.DataColumn>().Select(col => col.ColumnName)) .FirstOrDefault(); if (keyCol != null) { countriesDt.PrimaryKey = new System.Data.DataColumn[] { countriesDt.Columns[keyCol] }; } countriesDt.Columns.Add("WKT", typeof(string)); while (reader.Read()) { Feature feature = new Feature(); AttributesTable attributesTable = new AttributesTable(); string[] keys = new string[reader.DbaseHeader.NumFields]; IGeometry geometry = (Geometry)reader.Geometry; System.Data.DataRow row = countriesDt.NewRow(); for (int i = 0; i < reader.DbaseHeader.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = reader.DbaseHeader.Fields[i]; keys[i] = fldDescriptor.Name; attributesTable.AddAttribute(fldDescriptor.Name, reader.GetValue(i)); row[i] = reader.GetValue(i); } row["WKT"] = geometry.AsText(); countriesDt.Rows.Add(row); feature.Geometry = geometry; feature.Attributes = attributesTable; if (feature.Attributes.Exists("ISO2") && feature.Attributes.Exists("ISO3")) { int index = countryFeatures.Add(feature); var iso2 = (string)feature.Attributes["ISO2"]; var iso3 = (string)feature.Attributes["ISO3"]; iso2Countries.Add(iso2, feature); iso3Countries.Add(iso3, feature); } } reader.Close(); } }
public static List <Feature> ReadFeatures(string filename) { if (!File.Exists(filename)) { Console.WriteLine("The file " + filename + " does not exist."); return(new List <Feature>()); } var result = new List <Feature>(); ShapefileDataReader reader = TryShapefileDataReader(filename); if (reader == null) { return(new List <Feature>()); } try { while (reader.Read()) { var feature = new Feature(); var attr = new AttributesTable(); var geometry = (Geometry)reader.Geometry; for (int i = 0; i < reader.DbaseHeader.NumFields; ++i) { attr.Add(reader.DbaseHeader.Fields[i].Name, reader.GetValue(i + 1)); } feature.Geometry = geometry; feature.Attributes = attr; result.Add(feature); } } catch (Exception ex) { Console.WriteLine("Exception in feature read: " + ex.ToString() + ", filename: " + filename); } reader.Close(); reader.Dispose(); return(result); }
/// <summary> /// Convert shape-file to geojson-file stream /// </summary> /// <param name="shapeFilePath"></param> public static Stream ConvertToGeoJsonStream(string shapeFilePath) { var factory = new GeometryFactory(); //Create a feature collection var featureCollection = new FeatureCollection(); //Try to get crs from .prj file var crs = TryToGetCrs(shapeFilePath); //Set crs if we found it if (crs != CoordinateSystemId.None) { featureCollection.CRS = new NamedCRS(new CoordinateSystem(crs).Id.EpsgCode()); } using (var shapeFileDataReader = new ShapefileDataReader(shapeFilePath, factory)) { //Get shape file dbase header var header = shapeFileDataReader.DbaseHeader; //Loop throw all geometries while (shapeFileDataReader.Read()) { var attributesTable = new AttributesTable(); var geometry = (Geometry)shapeFileDataReader.Geometry; //Get header fields for (var i = 0; i < header.NumFields; i++) { var fldDescriptor = header.Fields[i]; attributesTable.AddAttribute(fldDescriptor.Name, shapeFileDataReader.GetValue(i)); } //Create feature using geometry and attributes var feature = new Feature() { Geometry = geometry, Attributes = attributesTable }; //Add feature to collection featureCollection.Features.Add(feature); } //Close and free up any resources shapeFileDataReader.Close(); } // Create a stream to write to. var outputStream = new MemoryStream(); var sw = new StreamWriter(outputStream); var jsonSerializer = new GeoJsonSerializer(factory); //Serialize feature collection to json jsonSerializer.Serialize(sw, featureCollection); //Flush stream writer and reset stream position sw.Flush(); outputStream.Position = 0; return(outputStream); }
public void Close() { _shapeFileDataReader.Close(); }
private string ExtractWKTFromShapefileZip(Stream stream, int points) { string uid = Guid.NewGuid().ToString(); string path = AppDomain.CurrentDomain.BaseDirectory; if (!path.EndsWith("/")) { path += "/"; } var shapeDir = path + "files/" + uid; if (stream.Length > 1 * 1000 * 1000) { throw new Exception("We only accept files < 1MB"); } using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read)) { archive.ExtractToDirectory(shapeDir); } //get all .shp files in unziped directory var myFiles = Directory.GetFiles(shapeDir, "*.shp", SearchOption.AllDirectories); GeometryFactory factory = new GeometryFactory(); NetTopologySuite.Geometries.Geometry finalgeometry = null; foreach (var shapefile in myFiles) { ShapefileDataReader shapeFileDataReader = new ShapefileDataReader(shapefile, factory); while (shapeFileDataReader.Read()) { var geometry = (NetTopologySuite.Geometries.Geometry)shapeFileDataReader.Geometry; if (finalgeometry == null) { finalgeometry = geometry; } else { finalgeometry = finalgeometry.Union(geometry); } } //Close and free up any resources shapeFileDataReader.Close(); shapeFileDataReader.Dispose(); } string wkt = null; if (finalgeometry != null) { finalgeometry = SimplifyGeometry(finalgeometry, points); foreach (var p in finalgeometry.Coordinates.ToArray()) { p.X = Math.Round(p.X, 2); p.Y = Math.Round(p.Y, 2); if (p is CoordinateZ) { p.Z = Math.Round(p.Z, 2); } } wkt = finalgeometry.AsText(); } return(wkt); }