public void FeatureCollectionSerialization() { var model = new FeatureCollection(); for (var i = 10; i-- > 0;) { var geom = new LineString(new[] { new GeographicPosition(51.010, -1.034), new GeographicPosition(51.010, -0.034) }); var props = new Dictionary<string, object> { { "test1", "1" }, { "test2", 2 } }; var feature = new Net.Feature.Feature(geom, props); model.Features.Add(feature); } var actualJson = JsonConvert.SerializeObject(model); Assert.IsNotNull(actualJson); Assert.IsFalse(string.IsNullOrEmpty(actualJson)); }
public void Can_Serialize_To_Null() { var collection = new FeatureCollection { CRS = new UnspecifiedCRS() }; var expectedJson = "{\"type\":\"FeatureCollection\",\"crs\":null,\"features\":[] }"; var actualJson = JsonConvert.SerializeObject(collection); JsonAssert.AreEqual(expectedJson, actualJson); }
public void NamedCrsSerializationNotSet() { var collection = new FeatureCollection(null); var serializedData = JsonConvert.SerializeObject(collection, DefaultJsonSerializerSettings); Assert.IsTrue(!serializedData.Contains("\"crs\"")); }
public static string FeatureCollectionToGeoJson(FeatureCollection featureCollection) { JFeature.FeatureCollection jFeatureCollection = FeatureCollection2JG(featureCollection); string actualJson = JsonConvert.SerializeObject(jFeatureCollection); return(actualJson); }
public IActionResult OnGetAirports() { var configuration = new Configuration { BadDataFound = context => { } }; using (var sr = new StreamReader(Path.Combine(_hostingEnvironment.WebRootPath, "airports.dat"))) using (var reader = new CsvReader(sr, configuration)) { GeoJSON.Net.Feature.FeatureCollection featureCollection = new GeoJSON.Net.Feature.FeatureCollection(); while (reader.Read()) { string name = reader.GetField <string>(1); string iataCode = reader.GetField <string>(4); double latitude = reader.GetField <double>(6); double longitude = reader.GetField <double>(7); featureCollection.Features.Add(new Feature( new GeoJSON.Net.Geometry.Point(new Position(latitude, longitude)), new Dictionary <string, object> { { "name", name }, { "iataCode", iataCode } })); } return(new JsonResult(featureCollection)); } }
public void GenerateMapData() { // init road data var roads = RoadSegment.ParseRoadData(File.ReadAllText(Path.Combine(dataDir, "r0.txt"))); // serialize as geojson FeatureCollection trafficData = new FeatureCollection(); foreach (RoadSegment road in roads) { // find the sensor and determine color var sensor = sensorData.Where(s => s.ID == road.ID).FirstOrDefault(); List<IPosition> segments = new List<IPosition>(); for (int i = 0; i < road.Nodes.Count; i++) { segments.Add(new GeographicPosition(road.Nodes[i].Lat, road.Nodes[i].Lon)); } var featureProperties = new Dictionary<string, object> { { "id", road.ID }, { "l", road.Semt }, { "s", sensor == null ? -1 : sensor.Speed } }; var feat = new Feature(new LineString(segments), featureProperties); trafficData.Features.Add(feat); } var serializedData = JsonConvert.SerializeObject(trafficData); var dateTime = String.Format("{2:00}/{3:00}/{4} {0:00}:{1:00}", acqTimeLocal.Hour, acqTimeLocal.Minute,acqTimeLocal.Day,acqTimeLocal.Month,acqTimeLocal.Year); StringBuilder sb = new StringBuilder(); sb.Append("var mapData=" + serializedData + ";\r\n"); sb.Append("var acqTime='" + dateTime + "';\r\n"); sb.Append("var trafficIndex=" + trafficIndex + ";\r\n"); File.WriteAllText(Path.Combine(dataDir, "tkmData.js"), sb.ToString()); }
public void Can_Serialize() { var collection = new FeatureCollection() { CRS = new NamedCRS("EPSG:31370") }; var actualJson = JsonConvert.SerializeObject(collection); JsonAssert.Contains("{\"properties\":{\"name\":\"EPSG:31370\"},\"type\":\"Name\"}", actualJson); }
public static JFeature.FeatureCollection FeatureCollection2JG(FeatureCollection featureCollection) { var jFeatures = (from feature in featureCollection.featureList select Feature2JG(feature)).ToList(); JFeature.FeatureCollection jFeatureCollection = new JFeature.FeatureCollection(); jFeatureCollection.Features.AddRange(jFeatures); return(jFeatureCollection); }
/// <summary> /// Prints GEOJson dump of Sites /// </summary> /// <param name="requiredProperties">list of properties to include (if not found in siteproperties an empty string will be inserted)</param> public void Execute(string[] requiredProperties, string siteType) { Console.Write("Content-Type: application/json\n\n"); var features = new List<Feature>(); FeatureCollection fc = new FeatureCollection(features); var filter = ""; if( siteType != "") filter = "type = '"+siteType+"'"; var sites = db.GetSiteCatalog(filter:filter); var siteProp = new TimeSeriesDatabaseDataSet.sitepropertiesDataTable(db); int id = 0; foreach (var s in sites) { try { var pos = new GeographicPosition(s.latitude, s.longitude); var pt = new GeoJSON.Net.Geometry.Point(pos); var props = siteProp.GetDictionary(s.siteid); for (int i = 0; i < requiredProperties.Length; i++) { if (requiredProperties[i].Trim() == "") continue; if (!props.ContainsKey(requiredProperties[i])) props.Add(requiredProperties[i], ""); } props.Add("siteid", s.siteid); props.Add("title", s.description); props.Add("state", s.state); props.Add("type", s.type); props.Add("region", s.responsibility.Trim()); props.Add("install", s.install); id++; var feature = new Feature(pt, props, id.ToString()); fc.Features.Add(feature); } catch (Exception error) { Console.WriteLine("Error at site:"+s); Console.WriteLine(error.Message); } } var settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; var json = Newtonsoft.Json.JsonConvert.SerializeObject(fc, Newtonsoft.Json.Formatting.Indented,settings); Console.WriteLine(json); //File.WriteAllText(@"c:\temp\test.json", json); }
protected override void AdditionalSetup() { var content = ResourceLoader.LoadSqlType("ng_with_circle"); var geoJson = GeoToSql.Translate(content); geoJson.ShouldBeOfType<FeatureCollection>(); newFeatureCollection = (FeatureCollection)geoJson; loadedGeoJson = GetObjectFromJson<FeatureCollection>("ng_with_circle"); }
private FeatureCollection GetFeatureCollection() { var model = new FeatureCollection(); for (var i = 10; i-- > 0;) { var geom = new LineString(new[] { new GeographicPosition(51.010, -1.034), new GeographicPosition(51.010, -0.034) }); var props = FeatureTests.GetPropertiesInRandomOrder(); var feature = new Net.Feature.Feature(geom, props); model.Features.Add(feature); } return model; }
private void Assert_Are_Equal(FeatureCollection left, FeatureCollection right) { Assert.AreEqual(left, right); Assert.IsTrue(left.Equals(right)); Assert.IsTrue(right.Equals(left)); Assert.IsTrue(left.Equals(left)); Assert.IsTrue(right.Equals(right)); Assert.IsTrue(left == right); Assert.IsTrue(right == left); Assert.IsFalse(left != right); Assert.IsFalse(right != left); Assert.AreEqual(left.GetHashCode(), right.GetHashCode()); }
public static List<LayerInfo> Parse(Stream stream, int x, int y, int z,bool convertToGeographicPosition=true) { var tile = Serializer.Deserialize<Tile>(stream); var list = new List<LayerInfo>(); foreach (var layer in tile.Layers) { var extent = layer.Extent; var li = new LayerInfo(); var fc = new FeatureCollection(); foreach (var feature in layer.Features) { var f = FeatureParser.Parse(feature, layer.Keys, layer.Values, x, y, z, extent, convertToGeographicPosition); if (f != null) { fc.Features.Add(f); } } li.FeatureCollection = fc; li.Name = layer.Name; list.Add(li); } return list; }
/// <summary> /// Returns the hash code for the specified object /// </summary> public int GetHashCode(FeatureCollection <TProps> other) { return(other.GetHashCode()); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // Create a new feature collections object, beacons will be represented by features FeatureCollection features = new FeatureCollection(); UIDocument uidoc = commandData.Application.ActiveUIDocument; Document doc = uidoc.Document; FamilyFilter ff = new FamilyFilter(); IList<Reference> sel = uidoc.Selection.PickObjects(ObjectType.Element, ff); //store the output data on desktop string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + doc.Title.Remove(doc.Title.Length - 4) + ".json"; //Overwrite the original file if action is duplicated using (StreamWriter sw = new StreamWriter(path, false)) { foreach (Reference r in sel) { try { Element e = doc.GetElement(r); FamilyInstance fi = e as FamilyInstance; LocationPoint lp = fi.Location as LocationPoint; // Create a new beacon and add it to the feature collection as a feature Beacon beacon = new Beacon(fi, lp); features.Features.Add(beacon.toGeoJSONFeature()); } catch (Exception e) { TaskDialog.Show("Revit", e.ToString()); } } // Convert the features collection to GeoJSON and output to external file sw.WriteLine(JsonConvert.SerializeObject(features)); } return Result.Succeeded; }
public void Ctor_Throws_ArgumentNullException_When_Features_Is_Null() { Assert.Throws<ArgumentNullException>(() => { var collection = new FeatureCollection(null); }); }
public void FeatureCollectionSerialization() { var model = new FeatureCollection(null); for (var i = 10; i-- > 0;) { var geom = new LineString(new[] { new GeographicPosition(51.010, -1.034), new GeographicPosition(51.010, -0.034), }); var props = new Dictionary<string, object>(); props.Add("test1", "1"); props.Add("test2", 2); var feature = new Feature.Feature(geom, props); model.Features.Add(feature); } var serialized = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); Assert.IsNotNull(serialized); Assert.IsFalse(string.IsNullOrEmpty(serialized)); }
public ToSqlGeometryTests() { point = new Point(new GeographicPosition(53.2455662, 90.65464646)); multiPoint = new MultiPoint(new List<Point> { new Point(new GeographicPosition(52.379790828551016, 5.3173828125)), new Point(new GeographicPosition(52.36721467920585, 5.456085205078125)), new Point(new GeographicPosition(52.303440474272755, 5.386047363281249, 4.23)) }); lineString = new LineString(new List<IPosition> { new GeographicPosition(52.379790828551016, 5.3173828125), new GeographicPosition(52.36721467920585, 5.456085205078125), new GeographicPosition(52.303440474272755, 5.386047363281249, 4.23) }); multiLineString = new MultiLineString(new List<LineString> { new LineString(new List<IPosition> { new GeographicPosition(52.379790828551016, 5.3173828125), new GeographicPosition(52.36721467920585, 5.456085205078125), new GeographicPosition(52.303440474272755, 5.386047363281249, 4.23) }), new LineString(new List<IPosition> { new GeographicPosition(52.379790828551016, 5.3273828125), new GeographicPosition(52.36721467920585, 5.486085205078125), new GeographicPosition(52.303440474272755, 5.426047363281249, 4.23) }) }); polygonWithHole = new Polygon(new List<LineString> { new LineString(new List<GeographicPosition> { new GeographicPosition(0.516357421875, 47.6415668949958), new GeographicPosition(0.516357421875, 47.34463879017405), new GeographicPosition(0.977783203125, 47.22539733216678), new GeographicPosition(1.175537109375, 47.463611506072866), new GeographicPosition(0.516357421875, 47.6415668949958) }), new LineString(new List<GeographicPosition> { new GeographicPosition(0.630340576171875, 47.54944962456812), new GeographicPosition(0.630340576171875, 47.49380564962583), new GeographicPosition(0.729217529296875, 47.482669772098674), new GeographicPosition(0.731964111328125, 47.53276262898896), new GeographicPosition(0.630340576171875, 47.54944962456812) }) }); polygon = new Polygon(new List<LineString> { new LineString(new List<GeographicPosition> { new GeographicPosition(52.379790828551016, 5.3173828125), new GeographicPosition(52.36721467920585, 5.456085205078125), new GeographicPosition(52.303440474272755, 5.386047363281249, 4.23), new GeographicPosition(52.379790828551016, 5.3173828125) }) }); multiPolygon = new MultiPolygon(new List<Polygon> { new Polygon(new List<LineString> { new LineString(new List<IPosition> { new GeographicPosition(52.959676831105995, -2.6797102391514338), new GeographicPosition(52.9608756693609, -2.6769029474483279), new GeographicPosition(52.908449372833715, -2.6079763270327119), new GeographicPosition(52.891287242948195, -2.5815104708998668), new GeographicPosition(52.875476700983896, -2.5851645010668989), new GeographicPosition(52.882954723868622, -2.6050779098387191), new GeographicPosition(52.875255907042678, -2.6373482332006359), new GeographicPosition(52.878791122091066, -2.6932445076063951), new GeographicPosition(52.89564268523565, -2.6931334629377890), new GeographicPosition(52.930592009390175, -2.6548779332193022), new GeographicPosition(52.959676831105995, -2.6797102391514338) }) }), new Polygon(new List<LineString> { new LineString(new List<IPosition> { new GeographicPosition(52.89610842810761, -2.69628632041613), new GeographicPosition(52.8894641454077, -2.75901233808515), new GeographicPosition(52.89938894657412, -2.7663172788742449), new GeographicPosition(52.90253773227807, -2.804554822840895), new GeographicPosition(52.929801009654575, -2.83848602260174), new GeographicPosition(52.94013913205788, -2.838979264607087), new GeographicPosition(52.937353122653533, -2.7978187468478741), new GeographicPosition(52.920394929466184, -2.772273870352612), new GeographicPosition(52.926572918779222, -2.6996509024137052), new GeographicPosition(52.89610842810761, -2.69628632041613) }) }) }); geomCollection = new GeometryCollection(new List<IGeometryObject> { point, multiPoint, lineString, multiLineString, polygon, multiPolygon }); feature = new Feature.Feature(polygon, new Dictionary<string, object>() { { "Key", "Value" } }, "Id"); featureCollection = new FeatureCollection(new List<Feature.Feature> { feature, new Feature.Feature(multiPolygon, null) }); }
public void NamedCrsHasCorrectType() { var collection = new FeatureCollection(null) { CRS = new NamedCRS("EPSG:31370") }; Assert.AreEqual(CRSType.Name, collection.CRS.Type); }
public void Ctor_Throws_ArgumentNullExpection_When_Name_Is_Empty() { Assert.Throws<ArgumentException>(() => { var collection = new FeatureCollection() { CRS = new NamedCRS(string.Empty) }; }); }
public void FeatureCollection_Test_IndexOf() { var model = new FeatureCollection(); var expectedIds = new List<string>(); var expectedIndexes = new List<int>(); for (var i = 0; i < 10; i++) { var id = "id" + i.ToString(); expectedIds.Add(id); expectedIndexes.Add(i); var geom = new LineString(new[] { new GeographicPosition(51.010, -1.034), new GeographicPosition(51.010, -0.034) }); var props = FeatureTests.GetPropertiesInRandomOrder(); var feature = new Net.Feature.Feature(geom, props, id); model.Features.Add(feature); } for (var i = 0; i < 10; i++) { var actualFeature = model.Features[i]; var actualId = actualFeature.Id; var actualIndex = model.Features.IndexOf(actualFeature); var expectedId = expectedIds[i]; var expectedIndex = expectedIndexes[i]; Assert.AreEqual(expectedId, actualId); Assert.AreEqual(expectedIndex, actualIndex); } }
/// <summary> /// Determines whether the specified object is equal to the current object /// </summary> public bool Equals(FeatureCollection <TProps> other) { return(Equals(this, other)); }
public void NamedCrsSerializationWithValue() { var collection = new FeatureCollection(null) { CRS = new NamedCRS("EPSG:31370") }; var serializedData = JsonConvert.SerializeObject(collection); Assert.IsTrue(serializedData.Contains("\"crs\":{\"type\":\"Name\",\"properties\":{\"name\":\"EPSG:31370\"}}")); }
public static string ToGeoJsonString(this GeoJSON.Net.Feature.FeatureCollection featureCollection) { return(JsonTools.SerializeObject(featureCollection)); }
public void NamedCrsSerializationEmpty() { var collection = new FeatureCollection(null) { CRS = new NamedCRS("") }; }
/// <summary> /// Creates the geojson export. /// </summary> /// <param name="results">The results.</param> /// <param name="exportProperties">The export properties.</param> public void CreateExport(IRecordSet results, string geometryFieldName) { if (results != null) { ICursor cursor; cursor = results.get_Cursor(false); IRow row = cursor.NextRow(); int rowCount = 0; //create an empty list of geojson features var geoJsonFeatures = new List<GeoJSON.Net.Feature.Feature>(); //get the index of the geometry field int fieldIndex = row.Fields.FindField(geometryFieldName); if (fieldIndex == -1) throw new Exception("Could not locate geometry field:shape"); if (row != null) { while (row != null) { //use the factory to convert the esri geometry to the geojson geometry IGeometryObject geoJsonGeom = null; if (row.get_Value(fieldIndex) != null) { GeoJsonGeometryFactory geomFactory = new GeoJsonGeometryFactory(); geoJsonGeom = geomFactory.GetGeometry(row.get_Value(fieldIndex) as IGeometry); } //use the factory to convert esri row to geojson attributes var atts = new GeoJsonAttributeFactory().GetAttributes(row); //create the feature and add it to the collection - use the unique key from the row or create one GeoJSON.Net.Feature.Feature f = null; if (row.Table.HasOID) { f = new GeoJSON.Net.Feature.Feature(geoJsonGeom, atts) { Id = row.get_Value(row.Fields.FindField(row.Table.OIDFieldName)).ToString() }; } else { f = new GeoJSON.Net.Feature.Feature(geoJsonGeom, atts) { Id = Guid.NewGuid().ToString() }; } geoJsonFeatures.Add(f); row = cursor.NextRow(); rowCount++; } } //initialise the geoJsonFeatureCollection var fc = new FeatureCollection(geoJsonFeatures); //to keep the export small, use formatting = none and exclude null values //todo - allow the null value handling and indentation to be exposed _geoJson = JsonConvert.SerializeObject(fc, Formatting.None, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore }); } }
private FeatureCollection ConvertGpxContentToGeoJson(byte[] content) { var collection = new FeatureCollection(); using (var stream = new MemoryStream(content)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(gpxType)); var gpx = xmlSerializer.Deserialize(stream) as gpxType; foreach (var point in gpx.wpt ?? new wptType[0]) { var feature = new Feature(new Point(CreateGeoPosition(point)), CreateNameProperties(point.name)); collection.Features.Add(feature); } foreach (var track in gpx.trk ?? new trkType[0]) { if (track.trkseg.Length == 1) { var lineStringFeature = new Feature(new LineString(track.trkseg[0].trkpt.Select(point => CreateGeoPosition(point))), CreateNameProperties(track.name)); collection.Features.Add(lineStringFeature); continue; } var lineStringList = new List<LineString>(); foreach (var segment in track.trkseg) { lineStringList.Add(new LineString(segment.trkpt.Select(point => CreateGeoPosition(point)))); } var feature = new Feature(new MultiLineString(lineStringList), CreateNameProperties(track.name)); collection.Features.Add(feature); } foreach (var route in gpx.rte ?? new rteType[0]) { var lineStringFeature = new Feature(new LineString(route.rtept.Select(point => CreateGeoPosition(point))), CreateNameProperties(route.name)); collection.Features.Add(lineStringFeature); } } return collection; }
public void Can_Serialize_Does_Not_Output_Crs_Property() { var collection = new FeatureCollection(); var json = JsonConvert.SerializeObject(collection); Assert.IsTrue(!json.Contains("\"crs\"")); }