Example #1
0
        private string ExtractWKTFromKML(Stream stream, int points)
        {
            NetTopologySuite.Geometries.Geometry finalgeometry = null;
            try {
                stream.Seek(0, SeekOrigin.Begin);
            }catch (Exception) {}
            KmlFile file = KmlFile.Load(stream);
            Kml     kml  = file.Root as Kml;

            var placemarks = kml.Flatten().OfType <Placemark>();

            foreach (var placemark in placemarks)
            {
                try {
                    NetTopologySuite.Geometries.Geometry geometry = KmlGeometryToGeometry(placemark.Geometry);
                    if (finalgeometry == null)
                    {
                        finalgeometry = geometry;
                    }
                    else
                    {
                        finalgeometry = finalgeometry.Union(geometry);
                    }
                } catch (Exception) {
                    //throw new Exception(string.Format("Error with placemark {0}", placemark.Name));
                }
            }
            string wkt = null;

            if (finalgeometry != null)
            {
                finalgeometry = SimplifyGeometry(finalgeometry, points);
                foreach (var p in finalgeometry.Coordinates.ToArray())
                {
                    p.X = Math.Round(p.X, 2);
                    p.Y = Math.Round(p.Y, 2);
                    if (p is CoordinateZ)
                    {
                        p.Z = Math.Round(p.Z, 2);
                    }
                }
                wkt = finalgeometry.AsText();
            }
            return(wkt);
        }
Example #2
0
        private string ExtractWKTFromShapefileZip(Stream stream, int points)
        {
            string uid  = Guid.NewGuid().ToString();
            string path = AppDomain.CurrentDomain.BaseDirectory;

            if (!path.EndsWith("/"))
            {
                path += "/";
            }

            var shapeDir = path + "files/" + uid;

            if (stream.Length > 1 * 1000 * 1000)
            {
                throw new Exception("We only accept files < 1MB");
            }
            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read)) {
                archive.ExtractToDirectory(shapeDir);
            }

            //get all .shp files in unziped directory
            var             myFiles = Directory.GetFiles(shapeDir, "*.shp", SearchOption.AllDirectories);
            GeometryFactory factory = new GeometryFactory();

            NetTopologySuite.Geometries.Geometry finalgeometry = null;

            foreach (var shapefile in myFiles)
            {
                ShapefileDataReader shapeFileDataReader = new ShapefileDataReader(shapefile, factory);
                while (shapeFileDataReader.Read())
                {
                    var geometry = (NetTopologySuite.Geometries.Geometry)shapeFileDataReader.Geometry;
                    if (finalgeometry == null)
                    {
                        finalgeometry = geometry;
                    }
                    else
                    {
                        finalgeometry = finalgeometry.Union(geometry);
                    }
                }
                //Close and free up any resources
                shapeFileDataReader.Close();
                shapeFileDataReader.Dispose();
            }

            string wkt = null;

            if (finalgeometry != null)
            {
                finalgeometry = SimplifyGeometry(finalgeometry, points);
                foreach (var p in finalgeometry.Coordinates.ToArray())
                {
                    p.X = Math.Round(p.X, 2);
                    p.Y = Math.Round(p.Y, 2);
                    if (p is CoordinateZ)
                    {
                        p.Z = Math.Round(p.Z, 2);
                    }
                }
                wkt = finalgeometry.AsText();
            }
            return(wkt);
        }