Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            // let's show you what's going on.
            OsmSharp.Logging.Logger.LogAction = (origin, level, message, parameters) =>
            {
                Console.WriteLine($"[{origin}] {level} - {message}");
            };

            await Download.Download.ToFile("http://planet.anyways.eu/planet/europe/luxembourg/luxembourg-latest.osm.pbf", "luxembourg-latest.osm.pbf");

            await using var fileStream = File.OpenRead("luxembourg-latest.osm.pbf");
            // create source stream.
            var source = new PBFOsmStreamSource(fileStream);

            // show progress.
            var progress = source.ShowProgress();

            // filter all power lines and keep all nodes.
            var 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 part of power lines 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.
            var features = filtered.ToFeatureSource();

            // filter out only linestrings.
            var lineStrings = from feature in features
                              where feature.Geometry is LineString
                              select feature;

            // build feature collection.
            var featureCollection = new FeatureCollection();
            var attributesTable   = new AttributesTable {
                { "type", "powerline" }
            };

            foreach (var feature in lineStrings)
            { // make sure there is a constant # of attributes with the same names before writing the shapefile.
                featureCollection.Add(new Feature(feature.Geometry, attributesTable));
            }

            // convert to shape.
            var header      = ShapefileDataWriter.GetHeader(featureCollection.First(), featureCollection.Count);
            var shapeWriter = new ShapefileDataWriter("luxembourg.shp", new GeometryFactory())
            {
                Header = header
            };

            shapeWriter.Write(featureCollection);
        }
Ejemplo n.º 2
0
        public static Geometry ToGeometry(this FeatureCollection features)
        {
            if (features == null)
            {
                throw new ArgumentNullException(nameof(features));
            }

            if (features.Count != 1)
            {
                throw new NotSupportedException($"Right now only one geometry pear feature is supported. Geometry count: {features.Count}");
            }

            return(features.First().Geometry);
        }
Ejemplo n.º 3
0
        public static byte[] Serialize(FeatureCollection fc, GeometryType geometryType, byte dimensions = 2, IList <ColumnMeta> columns = null)
        {
            var featureFirst = fc.First();

            if (columns == null && featureFirst.Attributes != null)
            {
                columns = featureFirst.Attributes.GetNames()
                          .Select(n => new ColumnMeta()
                {
                    Name = n, Type = ToColumnType(featureFirst.Attributes.GetType(n))
                })
                          .ToList();
            }
            using var memoryStream = new MemoryStream();
            Serialize(memoryStream, fc, geometryType, dimensions, columns);
            return(memoryStream.ToArray());
        }