/// <summary> /// Creates a GeoJson from all ProjectGeometries in the current project (session has to be set) /// </summary> /// <returns>GeoJson string</returns> private string createGeoJson() { string projectId = HttpContext.Session.GetString("Project"); if (projectId == null) { return(""); } //TODO chb //Project myProject = db.Projects.Include(m => m.Geometries).Where(m => m.ProjectId == new Guid(projectId)).FirstOrDefault(); Project myProject = db.Projects .Include(p => p.ProjectGroups).ThenInclude(m => m.Geometries) .Where(m => m.ProjectId == new Guid(projectId)).FirstOrDefault(); if (myProject == null) { return(""); } if (myProject != null) { NetTopologySuite.Features.FeatureCollection featureCollection = new NetTopologySuite.Features.FeatureCollection(); //TODO chb //foreach (ReferenceGeometry g in myProject.Geometries.Where(m => m.StatusId != StatusEnum.deleted)) foreach (ReferenceGeometry g in myProject.ProjectGroups.SelectMany(pg => pg.Geometries).Where(g => g.StatusId != StatusEnum.deleted)) { if (g.Point != null) { featureCollection.Add(getFeature(g, GeomType.Point)); } if (g.Line != null) { featureCollection.Add(getFeature(g, GeomType.Line)); } if (g.Polygon != null) { featureCollection.Add(getFeature(g, GeomType.Polygon)); } } var jsonSerializer = GeoJsonSerializer.Create(); var sw = new System.IO.StringWriter(); jsonSerializer.Serialize(sw, featureCollection); return(sw.ToString()); } else { return(null);// "No project found"; } }
/// <summary> /// Adds features from on collection to another. /// </summary> public static void Add(this NetTopologySuite.Features.FeatureCollection features, NetTopologySuite.Features.FeatureCollection featuresToAdd) { foreach (var feature in featuresToAdd.Features) { features.Add(feature); } }
static void ReadGeometryStream() { // let's show you what's going on. OsmSharp.Logging.Logger.LogAction = (origin, level, message, parameters) => { System.Console.WriteLine(string.Format("[{0}] {1} - {2}", origin, level, message)); }; // Download.ToFile("http://files.itinero.tech/data/OSM/planet/europe/luxembourg-latest.osm.pbf", "luxembourg-latest.osm.pbf").Wait(); using (System.IO.FileStream fileStream = System.IO.File.OpenRead(@"D:\username\Documents\Visual Studio 2017\Projects\OsmTilePrerenderer\OsmTilePrerenderer\Data\monaco-latest.osm.pbf")) { // create source stream. OsmStreamSource source = new PBFOsmStreamSource(fileStream); // show progress. OsmStreamSource progress = source.ShowProgress(); // filter all powerlines and keep all nodes. System.Collections.Generic.IEnumerable <OsmGeo> filtered = from osmGeo in progress where osmGeo.Type == OsmSharp.OsmGeoType.Node || (osmGeo.Type == OsmSharp.OsmGeoType.Way && osmGeo.Tags != null && osmGeo.Tags.Contains("power", "line")) select osmGeo; // convert to a feature stream. // WARNING: nodes that are partof powerlines will be kept in-memory. // it's important to filter only the objects you need **before** // you convert to a feature stream otherwise all objects will // be kept in-memory. OsmSharp.Geo.Streams.IFeatureStreamSource features = filtered.ToFeatureSource(); // filter out only linestrings. System.Collections.Generic.IEnumerable <NetTopologySuite.Features.IFeature> lineStrings = from feature in features where feature.Geometry is LineString select feature; // build feature collection. NetTopologySuite.Features.FeatureCollection featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (NetTopologySuite.Features.IFeature feature in lineStrings) { featureCollection.Add(feature); } // convert to geojson. string json = ToJson(featureCollection); // var st = new Mapsui.Providers.MemoryProvider(json); System.IO.File.WriteAllText("output.geojson", json); } }
/// <summary> /// Converts the given polygon enumerable to a feature collection. /// </summary> public static NetTopologySuite.Features.FeatureCollection ToFeatureCollection(this IEnumerable <LocalGeo.Polygon> polygons) { var featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (var polygon in polygons) { featureCollection.Add(new NetTopologySuite.Features.Feature( polygon.ToPolygon(), new NetTopologySuite.Features.AttributesTable())); } return(featureCollection); }
/// <summary> /// Converts the instructions to features. /// </summary> public static NetTopologySuite.Features.FeatureCollection ToFeatures(this IEnumerable <Instruction> instructions, Route route) { var featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (var instruction in instructions) { var attributes = new NetTopologySuite.Features.AttributesTable(); attributes.AddAttribute("text", instruction.Text); attributes.AddAttribute("type", instruction.Type); var location = route.Shape[instruction.Shape]; featureCollection.Add(new NetTopologySuite.Features.Feature( new NetTopologySuite.Geometries.Point(location.ToCoordinate()), attributes)); } return(featureCollection); }
/// <summary> /// Converts the given tree to a feature collection. /// </summary> public static NetTopologySuite.Features.FeatureCollection ToFeatureCollection(this Algorithms.Networks.Analytics.Trees.Models.Tree tree) { var featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (var treeEdge in tree.Edges) { var attributes = new NetTopologySuite.Features.AttributesTable(); attributes.AddAttribute("weight1", treeEdge.Weight1.ToInvariantString()); attributes.AddAttribute("weight2", treeEdge.Weight2.ToInvariantString()); attributes.AddAttribute("edge", treeEdge.EdgeId.ToInvariantString()); attributes.AddAttribute("previous_edge", treeEdge.PreviousEdgeId.ToInvariantString()); featureCollection.Add(new NetTopologySuite.Features.Feature( treeEdge.ToLineString(), attributes)); } return(featureCollection); }