public void GeoJsonWriterWriteAttributesTest() { AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); string actual = new GeoJsonWriter().Write(attributes); Assert.AreEqual("\"properties\":{\"test1\":\"value1\"}", actual); }
//GET /api/routing?from=31.8239,35.0375&to=31.8213,35.0965&type=f public async Task<IHttpActionResult> GetRouting(string from, string to, string type) { LineString lineString; var profile = ConvertProfile(type); var pointFrom = await GetGeographicPosition(from); var pointTo = await GetGeographicPosition(to); if (ModelState.IsValid == false) { return BadRequest(ModelState); } if (profile == ProfileType.None) { lineString = new LineString(new[] { pointFrom, pointTo }); } else { lineString = await _routingGateway.GetRouting(new RoutingGatewayRequest { From = from, To = to, Profile = profile, }); } var table = new AttributesTable(); table.AddAttribute("Name", "Routing from " + from + " to " + to + " profile type: " + profile); table.AddAttribute("Creator", "IsraelHikingMap"); var feature = new Feature(lineString, table); return Ok(new FeatureCollection(new Collection<IFeature> { feature })); }
private void TestWriteZMValuesShapeFile(bool testM) { var points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); var csFactory = DotSpatialAffineCoordinateSequenceFactory.Instance; var sequence = csFactory.Create(3, Ordinates.XYZM); for (var i = 0; i < 3; i++) { sequence.SetOrdinate(i, Ordinate.X, points[i].X); sequence.SetOrdinate(i, Ordinate.Y, points[i].Y); sequence.SetOrdinate(i, Ordinate.Z, 1 + i); if (testM) sequence.SetOrdinate(i, Ordinate.M, 11 + i); } var lineString = Factory.CreateLineString(sequence); var attributes = new AttributesTable(); attributes.AddAttribute("FOO", "Trond"); var feature = new Feature(Factory.CreateMultiLineString(new[] { lineString }), attributes); var features = new Feature[1]; features[0] = feature; var shpWriter = new ShapefileDataWriter("ZMtest", Factory) { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shpWriter.Write(features); // Now let's read the file and verify that we got Z and M back var factory = new GeometryFactory(DotSpatialAffineCoordinateSequenceFactory.Instance); using (var reader = new ShapefileDataReader("ZMtest", factory)) { reader.Read(); var geom = reader.Geometry; for (var i = 0; i < 3; i++) { var c = geom.Coordinates[i]; Assert.AreEqual(i + 1, c.Z); } if (testM) { sequence = ((ILineString)geom).CoordinateSequence; for (var i = 0; i < 3; i++) { Assert.AreEqual(sequence.GetOrdinate(i, Ordinate.M), 11 + i); } } // Run a simple attribute test too var v = reader.GetString(0); Assert.AreEqual(v, "Trond"); } }
private static object InternalReadJson(JsonReader reader, JsonSerializer serializer) { if (reader.TokenType != JsonToken.StartObject) throw new ArgumentException("Expected token '{' not found."); reader.Read(); AttributesTable attributesTable = new AttributesTable(); while (reader.TokenType == JsonToken.PropertyName) { string attributeName = (string) reader.Value; reader.Read(); object attributeValue; if (reader.TokenType == JsonToken.StartObject) { // inner object attributeValue = InternalReadJson(reader, serializer); } else { attributeValue = reader.Value; } reader.Read(); attributesTable.AddAttribute(attributeName, attributeValue); } return attributesTable; }
public void GeoJsonWriterWriteFeatureTest() { AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); string actual = new GeoJsonWriter().Write(feature); Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}", actual); }
public void GeoJsonWriterWriteFeatureCollectionTest() { AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); FeatureCollection featureCollection = new FeatureCollection(new Collection<IFeature> { feature }) { CRS = new NamedCRS("name1") }; string actual = new GeoJsonWriter().Write(featureCollection); Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", actual); }
private static object InternalReadJson(JsonReader reader, JsonSerializer serializer) { // TODO: refactor to remove check when reading TopoJSON if (reader.TokenType == JsonToken.StartArray) { reader.Read(); // move to first item IList<object> array = new List<object>(); do { object inner = InternalReadJson(reader, serializer); array.Add(inner); reader.Read(); // move to next item } while (reader.TokenType != JsonToken.EndArray); return array; } if (reader.TokenType != JsonToken.StartObject) throw new ArgumentException("Expected token '{' not found."); reader.Read(); AttributesTable attributesTable = new AttributesTable(); while (reader.TokenType == JsonToken.PropertyName) { string attributeName = (string)reader.Value; reader.Read(); object attributeValue; if (reader.TokenType == JsonToken.StartObject) { // inner object attributeValue = InternalReadJson(reader, serializer); } else if (reader.TokenType == JsonToken.StartArray) { reader.Read(); // move to first item IList<object> array = new List<object>(); do { object inner = InternalReadJson(reader, serializer); array.Add(inner); reader.Read(); // move to next item } while (reader.TokenType != JsonToken.EndArray); attributeValue = array; } else { attributeValue = reader.Value; } attributesTable.AddAttribute(attributeName, attributeValue); reader.Read(); } // TODO: refactor to remove check when reading TopoJSON if (reader.TokenType != JsonToken.EndObject) throw new ArgumentException("Expected token '}' not found."); return attributesTable; }
private IAttributesTable ConvertTags(TagsCollectionBase tags, long id) { var properties = tags.ToStringObjectDictionary(); properties.Add("osm_id", id); var table = new AttributesTable(); foreach (var key in properties.Keys) { table.AddAttribute(key, properties[key]); } return table; }
public void GeoJsonSerializerAttributesTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); GeoJsonSerializer serializer = new GeoJsonSerializer(); serializer.Serialize(writer, attributes); writer.Flush(); Assert.AreEqual("\"properties\":{\"test1\":\"value1\"}", sb.ToString()); }
public void GeoJsonSerializerFeatureTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); GeoJsonSerializer serializer = new GeoJsonSerializer(); serializer.Serialize(writer, feature); writer.Flush(); Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}", sb.ToString()); }
public async Task<FeatureCollection> PostGpsTrace(string url) { var response = await GetFile(url); var gpxBytes = await _dataContainerConverterService.Convert(response.Content, response.FileName, DataContainerConverterService.GPX); var gpx = gpxBytes.ToGpx().UpdateBounds(); var routingType = GetRoutingType(gpx); var gpxLines = GpxToLineStrings(gpx); var manipulatedLines = await ManipulateGpxIntoAddibleLines(gpxLines); var attributesTable = new AttributesTable(); attributesTable.AddAttribute("routingType", routingType); var features = manipulatedLines.Select(l => new Feature(ToWgs84LineString(l.Coordinates), attributesTable) as IFeature).ToList(); return new FeatureCollection(new Collection<IFeature>(features)); }
public void WriteJsonTest() { AttributesTableConverter target = new AttributesTableConverter(); StringBuilder sb = new StringBuilder(); JsonTextWriter writer = new JsonTextWriter(new StringWriter(sb)); AttributesTable value = new AttributesTable(); value.AddAttribute("test1", "value1"); value.AddAttribute("test2", "value2"); JsonSerializer serializer = new JsonSerializer(); target.WriteJson(writer, value, serializer); writer.Flush(); Assert.AreEqual("\"properties\":{\"test1\":\"value1\",\"test2\":\"value2\"}", sb.ToString()); }
public void WriteJsonWithArrayTest() { FeatureConverter target = new FeatureConverter(); StringBuilder sb = new StringBuilder(); JsonTextWriter writer = new JsonTextWriter(new StringWriter(sb)); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", new [] { "value1", "value2" }); IFeature value = new Feature(new Point(23, 56), attributes); JsonSerializer serializer = new GeoJsonSerializer(); target.WriteJson(writer, value, serializer); writer.Flush(); Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":[\"value1\",\"value2\"]}}", sb.ToString()); }
public void GeoJsonSerializerFeatureCollectionTest() { StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); FeatureCollection featureCollection = new FeatureCollection(new Collection<IFeature> {feature}) {CRS = new NamedCRS("name1")}; GeoJsonSerializer serializer = new GeoJsonSerializer(); serializer.Serialize(writer, featureCollection); writer.Flush(); Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", sb.ToString()); }
public void ok_when_writing_shapefile_with_features() { DbaseFileHeader header = new DbaseFileHeader(); header.AddColumn("X", 'C', 10, 0); ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36") { Header = header }; IAttributesTable attributesTable = new AttributesTable(); attributesTable.AddAttribute("X", "y"); IFeature feature = new Feature(new Point(1, 2), attributesTable); IList<IFeature> features = new List<IFeature>(); features.Add(feature); writer.Write(features); }
public void write_simple_point() { IPoint geometry = Factory.CreatePoint(new Coordinate(23.4, 56.7)); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("prop0", "value0"); attributes.AddAttribute("prop1", "value1"); IFeature feature = new Feature(geometry, attributes); TopoJsonWriter writer = new TopoJsonWriter(); string actual = writer.Write(feature); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.Not.Empty); Console.WriteLine(actual); Assert.That(actual, Is.EqualTo(TopoWriterData.SimplePoint)); }
/// <summary> /// Converts a <see cref="DataRow"/> into an <see cref="AttributesTable"/>. /// </summary> /// <param name="dataRow">A <see cref="DataRow"/></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>An <see cref="AttributesTable"/> containing the data in the input <see cref="DataRow"/>.</returns> public static AttributesTable ToAttributesTable(this DataRow dataRow, IEnumerable<string> omittedFields=null, IDictionary<string, string> aliases=null) { var attributesTable = new AttributesTable(); var columns = dataRow.Table.Columns; foreach (DataColumn column in columns) { if (omittedFields != null && omittedFields.Contains(column.ColumnName)) { continue; } attributesTable.AddAttribute(aliases != null && aliases.ContainsKey(column.ColumnName) ? aliases[column.ColumnName] : column.ColumnName, dataRow[column]); } return attributesTable; }
public void write_simple_polygon() { Coordinate c0 = new Coordinate(10.1, 10); Coordinate c1 = new Coordinate(20.2, 20); Coordinate c2 = new Coordinate(30.3, 30); IPolygon geometry = Factory.CreatePolygon(new[] { c0, c1, c2, c0 }); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("prop0", "value0"); attributes.AddAttribute("prop1", "value1"); IFeature feature = new Feature(geometry, attributes); TopoJsonWriter writer = new TopoJsonWriter(); string actual = writer.Write(feature); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.Not.Empty); Console.WriteLine(actual); Assert.That(actual, Is.EqualTo(TopoWriterData.SimplePolygon)); }
public void GeoJsonWriterWriteAnyObjectTest() { AttributesTable attributes = new AttributesTable(); DateTime Date = new DateTime(2012, 8, 8).Date; GeoJsonSerializer g = new GeoJsonSerializer(); StringBuilder sb = new StringBuilder(); using (StringWriter sw = new StringWriter(sb)) g.Serialize(sw, Date); string expectedDateString = sb.ToString(); string expectedResult = "{\"featureCollection\":{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}},\"Date\":" + expectedDateString + "}"; attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); FeatureCollection featureCollection = new FeatureCollection(new Collection<IFeature> { feature }) { CRS = new NamedCRS("name1") }; string actual = new GeoJsonWriter().Write(new { featureCollection, Date = Date }); Assert.AreEqual(expectedResult, actual); }
public void Setup() { ShapefileDataWriter sfdr = new ShapefileDataWriter("encoding_sample"); DbaseFileHeader h = new DbaseFileHeader(); h.AddColumn("id", 'n', 8, 0); h.AddColumn("Test", 'C', 15, 0); h.AddColumn("Ålder", 'N', 8, 0); h.AddColumn("Ödestext", 'C', 255, 0); h.NumRecords = 1; sfdr.Header = h; List<IFeature> feats = new List<IFeature>(); AttributesTable at = new AttributesTable(); at.AddAttribute("id", "0"); at.AddAttribute("Test", "Testar"); at.AddAttribute("Ålder", 10); at.AddAttribute("Ödestext", "Lång text med åäö etc"); feats.Add(new Feature(new Point(0, 0), at)); sfdr.Write(feats); }
private void TestShapeCreation() { var points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); var line_string = new LineString(points); var attributes = new AttributesTable(); attributes.AddAttribute("FOO", "FOO"); var feature = new Feature(Factory.CreateMultiLineString(new ILineString[] { line_string }), attributes); var features = new Feature[1]; features[0] = feature; var shp_writer = new ShapefileDataWriter("line_string") { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shp_writer.Write(features); }
public void ToGpx_WithAllTypesOffeatures_ShouldBeConverted() { var converter = new GpxGeoJsonConverter(); var geojson = new FeatureCollection(); var table = new AttributesTable(); geojson.Features.Add(new Feature(new Point(new Coordinate(1, 1)), table)); geojson.Features.Add(new Feature(new MultiPoint(new[] { new Point(new Coordinate(2,2)) as IPoint }), table)); geojson.Features.Add(new Feature(new Polygon(new LinearRing(new [] { new Coordinate(3,3), new Coordinate(4,4), new Coordinate(5,5), new Coordinate(3,3) })), table)); geojson.Features.Add(new Feature(new LineString(new[] { new Coordinate(6, 6), new Coordinate(7, 7) }), table)); geojson.Features.Add(new Feature(new MultiPolygon(new IPolygon[] { new Polygon(new LinearRing(new [] { new Coordinate(8,8), new Coordinate(9, 9), new Coordinate(10, 10), new Coordinate(8, 8) }))}), table)); geojson.Features.Add(new Feature(new MultiLineString(new ILineString[] { new LineString(new[] { new Coordinate(11, 11), new Coordinate(12, 12)}), new LineString(new[] { new Coordinate(12, 12), new Coordinate(13, 13)}), new LineString(new[] { new Coordinate(14, 14), new Coordinate(15, 15)}), }), table)); var gpx = converter.ToGpx(geojson); Assert.AreEqual(2, gpx.wpt.Length); Assert.AreEqual(3, gpx.rte.Length); Assert.AreEqual(2, gpx.trk.Length); }
public void write_polygon_with_hole() { Coordinate c0 = new Coordinate(10.1, 10); Coordinate c1 = new Coordinate(20.2, 20); Coordinate c2 = new Coordinate(30.3, 30); ILinearRing shell = Factory.CreateLinearRing(new[] { c0, c1, c2, c0 }); Coordinate h0 = new Coordinate(15, 15); Coordinate h1 = new Coordinate(17, 15); Coordinate h2 = new Coordinate(15, 17); ILinearRing hole = Factory.CreateLinearRing(new[] { h0, h1, h2, h0 }); IGeometry geometry = Factory.CreatePolygon(shell, new[] { hole }); AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("prop0", "value0"); attributes.AddAttribute("prop1", "value1"); IFeature feature = new Feature(geometry, attributes); TopoJsonWriter writer = new TopoJsonWriter(); string actual = writer.Write(feature); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.Not.Empty); Console.WriteLine(actual); Assert.That(actual, Is.EqualTo(TopoWriterData.PolygonWithHole)); }
public void WriteShapeFiles(DataAccessLayer DAL) { #if !(PocketPC || WindowsCE || Mobile) List<TtPolygon> polys = DAL.GetPolygons(); Dictionary<string, TtMetaData> metas = DAL.GetMetaData().ToDictionary(m => m.CN, m => m); string folder = SelectedPath + "\\GIS"; Directory.CreateDirectory(folder); foreach (TtPolygon poly in polys) { string _CurrDir = String.Format("{0}\\{1}\\", folder, poly.Name.ScrubFileName()); string _File = String.Format("{0}\\{1}\\{1}", folder, poly.Name.ScrubFileName()); Directory.CreateDirectory(_CurrDir); string CurrFileName = System.IO.Path.Combine(folder, poly.Name); List<TtPoint> points = DAL.GetPointsInPolygon(poly.CN).ToList(); List<WayPoint> wayPoints = points.FilterOnly(OpType.WayPoint).Cast<WayPoint>().ToList(); points = points.FilterOut(OpType.WayPoint).ToList(); TtMetaData md; if (points.Count > 0 || wayPoints.Count > 0) { if (points.Count > 0) md = DAL.GetMetaDataByCN(points[0].MetaDefCN); else md = DAL.GetMetaDataByCN(wayPoints[0].MetaDefCN); if (md == null) continue; } else continue; if (wayPoints.Count > 0) WriteWayPoints(_File, wayPoints, md); if (points.Count < 1) continue; CoordinateList BAdjCoords = new CoordinateList(); CoordinateList BUnAdjCoords = new CoordinateList(); CoordinateList NavAdjCoords = new CoordinateList(); CoordinateList NavUnAdjCoords = new CoordinateList(); bool hasWayPoints = false; TtPoint firstBndPoint = null, lastBntPoint = null; foreach (TtPoint point in points) { if (point.IsNavPoint()) { NavAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ)); NavUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ)); } if (point.IsBndPoint()) { BAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ)); BUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ)); if (firstBndPoint == null) firstBndPoint = point; lastBntPoint = point; } if (point.op == OpType.WayPoint) { hasWayPoints = true; } } double polyLinePerim = -1; if (firstBndPoint != null) polyLinePerim = poly.Perimeter - TtUtils.Distance(firstBndPoint, lastBntPoint, true); #region Navigation #region Adj string FileName = _File + "_NavAdj"; GeometryFactory geoFac = new GeometryFactory(); ShapefileDataWriter sdw; Polygonizer polyizer = new Polygonizer(); ArrayList features = new ArrayList(); AttributesTable attTable = new AttributesTable(); attTable.AddAttribute("Poly_Name", poly.Name); attTable.AddAttribute("Desc", poly.Description); attTable.AddAttribute("Poly", "Navigation Adjusted"); attTable.AddAttribute("CN", poly.CN); attTable.AddAttribute("Perim_M", poly.Perimeter); attTable.AddAttribute("PerimLine_M", polyLinePerim); Feature feat = new Feature(); DbaseFileHeader dbh; if (NavAdjCoords.Count > 1) { sdw = new ShapefileDataWriter(FileName, geoFac); feat.Geometry = new NetTopologySuite.Geometries.LineString(NavAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_NavAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation Adjusted Points"; features = GetPointFeatures(points.Where(p => p.IsNavPoint()), true, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region UnAdj if (NavUnAdjCoords.Count > 1) { FileName = _File + "_NavUnAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation UnAdjusted"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(NavUnAdjCoords.ToArray()); //feat.Geometry = new NetTopologySuite.Geometries.LinearRing(NavUnAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_NavUnAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation UnAdjusted Points"; features = GetPointFeatures(points.Where(p => p.IsNavPoint()), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #endregion #region Boundary #region Adj Line if (BAdjCoords.Count > 1) { FileName = _File + "_BndAdjLine"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary Adjusted Line"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(BAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } #endregion #region UnAdj Line if (BUnAdjCoords.Count > 1) { FileName = _File + "_BndUnAdjLine"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted Line"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(BUnAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } #endregion #region Adj if (BAdjCoords.Count > 3) { FileName = _File + "_BndAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable.AddAttribute("Area_MtSq", poly.Area); attTable["Poly"] = "Boundary Adjusted"; feat = new Feature(); if (BAdjCoords[0] != BAdjCoords[BAdjCoords.Count - 1]) BAdjCoords.Add(BAdjCoords[0]); feat.Geometry = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(BAdjCoords.ToArray())); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_BndAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary Adjusted Points"; features = GetPointFeatures(points.Where(p => p.IsBndPoint()), true, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region UnAdj if (BUnAdjCoords.Count > 3) { FileName = _File + "_BndUnAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted"; feat = new Feature(); if (BUnAdjCoords[0] != BUnAdjCoords[BUnAdjCoords.Count - 1]) BUnAdjCoords.Add(BUnAdjCoords[0]); feat.Geometry = new NetTopologySuite.Geometries.Polygon(new LinearRing(BUnAdjCoords.ToArray())); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_BndUnAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted Points"; features = GetPointFeatures(points.Where(p => p.IsBndPoint()), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region WayPoints if (hasWayPoints) { //points FileName = _File + "_WayPoints"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "WayPoints"; features = GetPointFeatures(points.Where(p => p.op == OpType.WayPoint), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #endregion } #endif }
public bool ImportShapes(List<string> files, bool latlon, bool useShapeProps, bool useElev, bool elevFeet) { GeometryFactory factory; ShapefileDataReader shapeFileDataReader; ArrayList features; Feature feature; AttributesTable attributesTable; string[] keys; Geometry geometry; DbaseFieldDescriptor fldDescriptor; int polyCount = (int)dal.GetPolyCount(); GpsPoint gps; int index = 0; _Polygons = new Dictionary<string, TtPolygon>(); _Points = new List<TtPoint>(); List<TtPoint> tmpPoints = new List<TtPoint>(); try { foreach (string file in files) { //polyCount = 1; factory = new GeometryFactory(); shapeFileDataReader = new ShapefileDataReader(file, factory); DbaseFileHeader header = shapeFileDataReader.DbaseHeader; features = new ArrayList(); while (shapeFileDataReader.Read()) { feature = new Feature(); attributesTable = new AttributesTable(); keys = new string[header.NumFields]; geometry = (Geometry)shapeFileDataReader.Geometry; for (int i = 0; i < header.NumFields; i++) { fldDescriptor = header.Fields[i]; keys[i] = fldDescriptor.Name; attributesTable.AddAttribute(fldDescriptor.Name, shapeFileDataReader.GetValue(i)); } feature.Geometry = geometry; feature.Attributes = attributesTable; features.Add(feature); } bool areAllPoints = true; foreach (Feature feat in features) { if (feat.Geometry.GeometryType.ToLower() != "point") { areAllPoints = false; break; } } //if all features are points if (areAllPoints) { tmpPoints.Clear(); _Poly = new TtPolygon(1000 * polyCount + 10); _Poly.Name = Path.GetFileNameWithoutExtension(file); index = 0; foreach (Feature feat in features) { //if features is only a point there should only be 1 coord foreach (Coordinate coord in feat.Geometry.Coordinates) { gps = new GpsPoint(); gps.OnBnd = true; gps.Index = index; index++; gps.MetaDefCN = _Meta.CN; if (tmpPoints.Count > 0) gps.PID = PointNaming.NamePoint(tmpPoints.Last(), _Poly); else gps.PID = PointNaming.NameFirstPoint(_Poly); if (latlon) { double x,y; TtUtils.LatLontoUTM(coord.Y, coord.X, _Meta.Zone, out y, out x); gps.UnAdjX = x; gps.UnAdjY = y; } else { gps.UnAdjX = coord.X; gps.UnAdjY = coord.Y; } if (useElev) { if (coord.Z != double.NaN) { if (elevFeet) gps.UnAdjZ = TtUtils.ConvertToMeters(coord.Z, Unit.FEET_TENTH); else gps.UnAdjZ = coord.Z; } else gps.UnAdjZ = 0; } else gps.UnAdjZ = 0; gps.PolyCN = _Poly.CN; gps.PolyName = _Poly.Name; gps.GroupCN = Values.MainGroup.CN; gps.GroupName = Values.MainGroup.Name; tmpPoints.Add(gps); } _Points.AddRange(tmpPoints); } _Polygons.Add(_Poly.CN, _Poly); polyCount++; } else //else get points out of each features { int fidInc = 0; foreach (Feature feat in features) { tmpPoints.Clear(); _Poly = new TtPolygon(1000 * polyCount + 10); if (features.Count < 2) _Poly.Name = Path.GetFileNameWithoutExtension(file); else _Poly.Name = String.Format("{0}-{1}", fidInc++, Path.GetFileNameWithoutExtension(file)); #region Shape Desc Properties if (useShapeProps) { object[] objs = feat.Attributes.GetValues(); string[] names = feat.Attributes.GetNames(); string objv; for (int i = 0; i < feat.Attributes.Count; i++) { if (objs[i] is string) { objv = (string)objs[i]; if (objv.IsEmpty()) continue; switch (names[i].ToLower()) { case "description": case "comment": case "poly": if (_Poly.Description.IsEmpty()) _Poly.Description = objv; else _Poly.Description = String.Format("{0} | {1}", _Poly.Description, objv); break; case "name": case "unit": _Poly.Name = objv; break; } } } } #endregion index = 0; foreach (Coordinate coord in feat.Geometry.Coordinates) { gps = new GpsPoint(); gps.OnBnd = true; gps.Index = index; index++; gps.MetaDefCN = _Meta.CN; if (tmpPoints.Count > 0) gps.PID = PointNaming.NamePoint(tmpPoints.Last(), _Poly); else gps.PID = PointNaming.NameFirstPoint(_Poly); if (latlon) { double x, y; TtUtils.LatLontoUTM(coord.Y, coord.X, _Meta.Zone, out y, out x); gps.UnAdjX = x; gps.UnAdjY = y; } else { gps.UnAdjX = coord.X; gps.UnAdjY = coord.Y; } if (useElev) { if (coord.Z == double.NaN) { if (elevFeet) gps.UnAdjZ = TtUtils.ConvertToMeters(coord.Z, Unit.FEET_TENTH); else gps.UnAdjZ = coord.Z; } else gps.UnAdjZ = 0; } else gps.UnAdjZ = 0; gps.PolyCN = _Poly.CN; gps.PolyName = _Poly.Name; gps.GroupCN = Values.MainGroup.CN; gps.GroupName = Values.MainGroup.Name; tmpPoints.Add(gps); } _Points.AddRange(tmpPoints); _Polygons.Add(_Poly.CN, _Poly); polyCount++; } } //Close and free up any resources shapeFileDataReader.Close(); shapeFileDataReader.Dispose(); } foreach (TtPolygon poly in _Polygons.Values) dal.InsertPolygon(poly); dal.InsertPoints(_Points); PolygonAdjuster.Adjust(dal); } catch (Exception ex) { TtUtils.WriteError(ex.Message, "DataImport:ImportShapes", ex.StackTrace); return false; } return true; }
private ArrayList GetPointFeatures(IEnumerable<TtPoint> points, bool adjusted, DataAccessLayer DAL, Dictionary<string, TtMetaData> metas) { ArrayList features = new ArrayList(); Feature feat; AttributesTable attPointTable = new AttributesTable(); foreach (TtPoint p in points) { TtMetaData tmpMeta = metas[p.MetaDefCN]; attPointTable = new AttributesTable(); attPointTable.AddAttribute("PID", p.PID); attPointTable.AddAttribute("Op", p.op.ToString()); attPointTable.AddAttribute("Index", p.Index); attPointTable.AddAttribute("PolyName", p.PolyName); attPointTable.AddAttribute("DateTime", p.Time.ToString("MM/dd/yyyy hh:mm:ss tt")); /* if (metas.ContainsKey(p.MetaDefCN)) attPointTable.AddAttribute("MetaData", tmpMeta.Name); else attPointTable.AddAttribute("MetaData", String.Empty); */ attPointTable.AddAttribute("OnBnd", p.OnBnd); //attPointTable.AddAttribute("GroupName", p.GroupName); attPointTable.AddAttribute("AdjX", p.AdjX); attPointTable.AddAttribute("AdjY", p.AdjY); attPointTable.AddAttribute("AdjZ", p.AdjZ); attPointTable.AddAttribute("UnAdjX", p.UnAdjX); attPointTable.AddAttribute("UnAdjY", p.UnAdjY); attPointTable.AddAttribute("UnAdjZ", p.UnAdjZ); if (p.IsGpsType()) { GpsPoint gps = (GpsPoint)p; //attPointTable.AddAttribute("X", gps.X); //attPointTable.AddAttribute("Y", gps.X); //attPointTable.AddAttribute("Z", gps.Z); attPointTable.AddAttribute("RMSEr", gps.RMSEr.dNoNull()); attPointTable.AddAttribute("ManAcc", gps.ManualAccuracy.dNoNull()); } else { //attPointTable.AddAttribute("X", String.Empty); //attPointTable.AddAttribute("Y", String.Empty); //attPointTable.AddAttribute("Z", String.Empty); attPointTable.AddAttribute("RMSEr", String.Empty); attPointTable.AddAttribute("ManAcc", String.Empty); } /* if (p.IsTravType()) { SideShotPoint ssp = (SideShotPoint)p; attPointTable.AddAttribute("FwdAz", ssp.ForwardAz.dNoNull()); attPointTable.AddAttribute("BkAz", ssp.BackwardAz.dNoNull()); attPointTable.AddAttribute("SlopeDist", TtUtils.ConvertDistance(ssp.SlopeDistance, tmpMeta.uomDistance, UomDistance.Meters).ToString()); attPointTable.AddAttribute("HorzDist", TtUtils.ConvertDistance(ssp.HorizontalDistance, tmpMeta.uomDistance, UomDistance.Meters).ToString()); attPointTable.AddAttribute("DistType", tmpMeta.uomDistance.ToString()); attPointTable.AddAttribute("SlopeAngle", ssp.SlopeAngle); attPointTable.AddAttribute("AngleType", tmpMeta.uomSlope.ToString()); } else { attPointTable.AddAttribute("FwdAz", String.Empty); attPointTable.AddAttribute("BkAz", String.Empty); attPointTable.AddAttribute("SlopeDist", String.Empty); attPointTable.AddAttribute("HorzDist", String.Empty); attPointTable.AddAttribute("DistType", String.Empty); attPointTable.AddAttribute("SlopeAngle", String.Empty); attPointTable.AddAttribute("AngleType", String.Empty); } */ if (p.op == OpType.Quondam) { QuondamPoint q = (QuondamPoint)p; attPointTable.AddAttribute("ParentName", q.ParentPID); } else { attPointTable.AddAttribute("ParentName", String.Empty); } attPointTable.AddAttribute("Comment", p.Comment == null ? String.Empty : p.Comment); feat = new Feature(); feat.Geometry = (adjusted) ? new NetTopologySuite.Geometries.Point(p.AdjX, p.AdjY, p.AdjZ) : new NetTopologySuite.Geometries.Point(p.UnAdjX, p.UnAdjY, p.UnAdjZ); feat.Attributes = attPointTable; features.Add(feat); } return features; }
private IAttributesTable CreateNameProperties(string name) { var table = new AttributesTable(); table.AddAttribute(NAME, name); return table; }
// see https://code.google.com/p/nettopologysuite/issues/detail?id=146 public void Issue146_ShapeCreationWithInvalidAttributeName() { Coordinate[] points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); LineString ls = new LineString(points); IMultiLineString mls = GeometryFactory.Default.CreateMultiLineString(new ILineString[] { ls }); AttributesTable attrs = new AttributesTable(); attrs.AddAttribute("Simulation name", "FOO"); Feature[] features = new[] { new Feature(mls, attrs) }; ShapefileDataWriter shp_writer = new ShapefileDataWriter("invalid_line_string") { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shp_writer.Write(features); }
private void WriteWayPoints(string file, List<WayPoint> points, TtMetaData md) { #if !(PocketPC || WindowsCE || Mobile) string FileName = file + "_WayPoints"; GeometryFactory geoFac = new GeometryFactory(); ShapefileDataWriter sdw = new ShapefileDataWriter(FileName, geoFac); ArrayList features = new ArrayList(); AttributesTable attTable; Feature feat = new Feature(); DbaseFileHeader dbh; foreach (WayPoint point in points) { feat = new Feature(); attTable = new AttributesTable(); attTable.AddAttribute("PID", point.PID); attTable.AddAttribute("Comment", point.Comment ?? String.Empty); attTable.AddAttribute("CN", point.CN); attTable.AddAttribute("Group", point.GroupName ?? String.Empty); attTable.AddAttribute("RMSEr", point.RMSEr ?? -1); attTable.AddAttribute("UnAdjX", point.UnAdjX); attTable.AddAttribute("UnAdjY", point.UnAdjY); attTable.AddAttribute("UnAdjZ", point.UnAdjZ); feat.Geometry = new NetTopologySuite.Geometries.Point(point.UnAdjX, point.UnAdjY, point.UnAdjZ); feat.Attributes = attTable; features.Add(feat); } dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); #endif }
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); }