static void Add(FeatureCollection featureCollection, IFeature feature) { if (feature.Geometry.GeometryType == "MultiPoint") { var multiPoint = (MultiPoint)feature.Geometry; foreach (var point in multiPoint.Geometries) { var featurePoint = new Feature(point, feature.Attributes); featureCollection.Add(featurePoint); } return; } featureCollection.Add(feature); }
public void AddTest() { FeatureCollection target = new FeatureCollection(); IFeature feature = new Feature(); target.Add(feature); Assert.AreEqual(feature, target.Features[0]); }
public static FeatureCollection FromFlatGeobuf(byte[] bytes) { var fc = new NetTopologySuite.Features.FeatureCollection(); var bb = new ByteBuffer(bytes); var headerSize = (ulong)ByteBufferUtil.GetSizePrefix(bb); bb.Position = FlatBufferConstants.SizePrefixLength; var header = Header.GetRootAsHeader(bb); var count = header.FeaturesCount; var featuresSize = header.FeaturesSize; var nodeSize = header.IndexNodeSize; bb.Position += (int)headerSize; var index = new PackedHilbertRTree(count, nodeSize); var indexData = bytes.Skip((int)(headerSize + featuresSize)).Take((int)index.Size).ToArray(); index.Load(indexData); while (count-- > 0) { var featureLength = ByteBufferUtil.GetSizePrefix(bb); bb.Position += FlatBufferConstants.SizePrefixLength; var feature = FeatureConversions.FromByteBuffer(bb, header); fc.Add(feature); bb.Position += featureLength; } return(fc); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; reader.Read(); FeatureCollection fc = new FeatureCollection(); while (reader.TokenType != JsonToken.EndObject) { if (reader.TokenType != JsonToken.PropertyName) throw new ArgumentException("Expected a property name."); string val = (string)reader.Value; if (val == "features") { reader.Read(); if (reader.TokenType != JsonToken.StartArray) throw new ArgumentException("Expected token '[' not found."); reader.Read(); while (reader.TokenType != JsonToken.EndArray) fc.Add(serializer.Deserialize<Feature>(reader)); reader.Read(); continue; } if (val == "type") { reader.Read(); if (reader.TokenType != JsonToken.String && (string) reader.Value != "FeatureCollection") throw new ArgumentException("Expected value 'FeatureCollection' not found."); reader.Read(); continue; } if (val == "bbox") { reader.Read(); if (reader.TokenType != JsonToken.StartArray) throw new ArgumentException("Expected token '{' not found."); // read the values but for now discard them var env = serializer.Deserialize<double[]>(reader); if (reader.TokenType != JsonToken.EndArray) throw new ArgumentException("Expected token '}' not found."); reader.Read(); continue; } if (val == "crs") { reader.Read(); fc.CRS = serializer.Deserialize<ICRSObject>(reader); continue; } // additional members are ignored: see https://code.google.com/p/nettopologysuite/issues/detail?id=186 reader.Read(); // read property value reader.Read(); // move next } return fc; }
public static FeatureCollection Deserialize(byte[] bytes) { var fc = new NetTopologySuite.Features.FeatureCollection(); foreach (var feature in Deserialize(new MemoryStream(bytes))) { fc.Add(feature); } return(fc); }
public static FeatureCollection FromFlatGeobuf(byte[] bytes) { var fc = new NetTopologySuite.Features.FeatureCollection(); var bb = new ByteBuffer(bytes); var headerSize = ByteBufferUtil.GetSizePrefix(bb); bb.Position = FlatBufferConstants.SizePrefixLength; var header = Header.GetRootAsHeader(bb); var count = header.FeaturesCount; var nodeSize = header.IndexNodeSize; var geometryType = header.GeometryType; var dimensions = header.Dimensions; IList <ColumnMeta> columns = null; if (header.ColumnsLength > 0) { columns = new List <ColumnMeta>(); for (int i = 0; i < header.ColumnsLength; i++) { var column = header.Columns(i).Value; columns.Add(new ColumnMeta() { Name = column.Name, Type = column.Type }); } } bb.Position += headerSize; if (nodeSize > 0) { var index = new PackedHilbertRTree(count, nodeSize); var indexData = bytes.Skip(headerSize).Take((int)index.Size).ToArray(); index.Load(indexData); bb.Position += (int)index.Size + (int)count * 8; } while (bb.Position < bb.Length) { var featureLength = ByteBufferUtil.GetSizePrefix(bb); bb.Position += FlatBufferConstants.SizePrefixLength; var feature = FeatureConversions.FromByteBuffer(bb, geometryType, dimensions, columns); fc.Add(feature); bb.Position += featureLength; } return(fc); }
/// <summary> /// Converts DotSpatial features into a NetTopologySuite feature collection. /// </summary> /// <param name="features">DotSpatial features.</param> /// <param name="outSR">The EPSG identifier for a spatial reference system.</param> /// <param name="omittedFields">Names of fields that will be omitted.</param> /// <param name="aliases">This parameter allows you to provide aliases for the field names.</param> /// <returns>A NetTopologySuite feature collection</returns> public static FeatureCollection ToNtsFeatureCollection(this IEnumerable<DotSpatial.Data.IFeature> features, int outSR, IEnumerable<string> omittedFields = null, IDictionary<string, string> aliases = null) { var featureCollection = new FeatureCollection(); // Omit CRS for WGS 84, which is the default for GeoJSON. if (outSR != 4326) { featureCollection.CRS = new NamedCRS(string.Format("urn:ogc:def:crs:EPSG::{0}", outSR)); } foreach (var f in features.AsNtsFeatures(omittedFields, aliases)) { featureCollection.Add(f); } return featureCollection; }
/// <summary> /// Returns MarkerType property for legend. /// </summary> /// <param name="colorRepo"></param> /// <param name="resultDir"></param> /// <param name="fi"></param> /// <param name="resultName"></param> /// <returns></returns> private static void ProcessVectorFile(IColorRepository colorRepo, DirectoryInfo resultDir, FileInfo fi, string resultName) { string localFileName = fi.Name; string localResultName = resultName; Console.WriteLine(string.Format("Processing {0} into {1}...", localFileName, localResultName)); StringBuilder bldr = new StringBuilder(); NetTopologySuite.IO.ShapefileDataReader dataReader = new NetTopologySuite.IO.ShapefileDataReader(fi.FullName, new GeometryFactory()); NetTopologySuite.Features.FeatureCollection featureCollection = new NetTopologySuite.Features.FeatureCollection(); List<string> csvHdr = dataReader.DbaseHeader.Fields.Select(a => a.Name).ToList(); csvHdr.Add(appColorNamspace); bldr.AppendLine(string.Join(",", csvHdr)); //write csv file header while (dataReader.Read()) { NetTopologySuite.Features.Feature feature = new NetTopologySuite.Features.Feature(); feature.Geometry = dataReader.Geometry; int numFields = dataReader.DbaseHeader.NumFields + 1; string[] keys = new string[numFields]; int colorValueField = -1; for (int i = 0; i < numFields - 1; i++) { keys[i] = dataReader.DbaseHeader.Fields[i].Name; if (keys[i].Equals(colorRepo.ColorFieldForOutput(localFileName, localResultName))) { colorValueField = i; } } //add attributes from source attribute table feature.Attributes = new AttributesTable(); List<string> csvLine = new List<string>(); for (int i = 0; i < numFields - 1; i++) { object val = dataReader.GetValue(i); feature.Attributes.AddAttribute(keys[i], val); csvLine.Add(val.ToString()); } if (colorRepo.MapColorsToThisResult(localFileName, localResultName)) { //mark outline colors in a different attribute than fill colors string colorNs = colorRepo.IsOutlinedNotFilled(localFileName, localResultName) ? appOutlineNamespace : appColorNamspace; keys[numFields - 1] = colorNs; //add additional attribute for color binding string hexClr = colorRepo.SingleColorForFile(localFileName, localResultName); //only path where colorValueField, i.e. ColorMap.clrField can be unpopulated. if (string.IsNullOrEmpty(hexClr) && colorValueField > -1) { if (colorRepo.IsCategoricalMap(localFileName, resultName)) { //categorical color map hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetString(colorValueField)).HexColor; } else { //numerical range color map hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetDouble(colorValueField)).HexColor; } } if (string.IsNullOrEmpty(hexClr)) // else if (string.IsNullOrEmpty(hexClr) && colorValueField < 0) { throw new NotSupportedException("Cannot color a file with no attributes to bind to and no single-color given."); } csvLine.Add(hexClr); feature.Attributes.AddAttribute(colorNs, hexClr); } bldr.AppendLine(string.Join(",", csvLine)); featureCollection.Add(feature); } GeoJsonWriter wtr = new GeoJsonWriter(); string layerJson = wtr.Write(featureCollection); File.WriteAllText(resultDir.FullName + localResultName, layerJson); File.WriteAllText(resultDir.FullName + localResultName.Replace(".json", ".csv"), bldr.ToString()); }
public static string ToGeoJson(IEnumerable<System.Data.Entity.Spatial.DbGeometry> dbGeometrys, ProjectionInfo pStart, DataTable dataTable) { var pEnd = Definitions.WorldProjection; var dbGeometrys1 = dbGeometrys as IList<System.Data.Entity.Spatial.DbGeometry> ?? dbGeometrys.ToList(); var reader = new WKTReader(); var featureCollection = new FeatureCollection(); var columns = (from DataColumn column in dataTable.Columns select column.ColumnName).ToList(); for (var i = 0; i < dbGeometrys1.Count(); i++) { var geometry = GeometryHelper.Project(dbGeometrys1[i].MakeValid(), pStart, pEnd); var read = reader.Read(geometry.WellKnownValue.WellKnownText); var table = new AttributesTable(); foreach (var column in columns) { table.AddAttribute(column, dataTable.Rows[i][column]); } featureCollection.Add(new Feature(read, table)); } var geoJsonWriter = new GeoJsonWriter(); return geoJsonWriter.Write(featureCollection); }
private static FeatureCollection FeatureCollection(string geoJson) { var reader = new GeoJsonReader(); var featureCollection = new FeatureCollection(); try { featureCollection = reader.Read<FeatureCollection>(geoJson); if (featureCollection.Count == 0) { var feature = reader.Read<Feature>(geoJson); featureCollection.Add(feature); } } catch (Exception) { var feature = reader.Read<Feature>(geoJson); featureCollection.Add(feature); } return featureCollection; }
private FeatureCollection CreateCollection(TopoObject[] data) { IFeature[] features = new IFeature[data.Length]; for (int i = 0; i < data.Length; i++) { TopoObject component = data[i]; FeatureCollection coll = Create(component); features[i] = coll[0]; } FeatureCollection collection = new FeatureCollection(); foreach (IFeature feature in features) collection.Add(feature); return collection; }