Esempio n. 1
0
        static public Polygon ToPolygon(this GeoJSON.Net.Geometry.MultiPolygon geoJsonMultiPolygon)
        {
            var polygon = new Polygon();

            if (geoJsonMultiPolygon?.Coordinates != null)
            {
                foreach (var geoJsonPolygon in geoJsonMultiPolygon.Coordinates)
                {
                    if (geoJsonPolygon?.Coordinates != null)
                    {
                        foreach (var geoJsonLineString in geoJsonPolygon.Coordinates)
                        {
                            if (geoJsonLineString?.Coordinates != null)
                            {
                                var ring = new Ring();
                                polygon.AddRing(ring);

                                foreach (var position in geoJsonLineString.Coordinates)
                                {
                                    ring.AddPoint(new Point(position.Longitude, position.Latitude));
                                }
                            }
                        }
                    }
                }
            }

            return(polygon);
        }
        private Feature Map(FieldBoundary fieldBoundary)
        {
            GeoJSON.Net.Geometry.MultiPolygon multiPolygon = MultiPolygonMapper.Map(fieldBoundary.SpatialData, _properties.AffineTransformation);
            if (multiPolygon == null)
            {
                return(null);
            }

            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("Guid", UniqueIdMapper.GetUniqueGuid(fieldBoundary.Id, UniqueIdSourceCNH));

            if (_properties.Anonymise)
            {
                properties.Add("Description", "Field boundary " + fieldBoundary.Id.ReferenceId);
            }
            else
            {
                properties.Add("Description", fieldBoundary.Description);
            }

            // GpsSource
            var gpsSource = fieldBoundary.GpsSource;

            properties.Add("GpsSource", null);
            if (gpsSource != null)
            {
                properties["GpsSource"] = fieldBoundary.GpsSource.ToString();
            }

            // Created time
            var creationTime = fieldBoundary.TimeScopes.Where(ts => ts.DateContext == DateContextEnum.Creation).FirstOrDefault();

            properties.Add("CreationTime", null);
            if (creationTime != null)
            {
                if (creationTime.TimeStamp1 != null)
                {
                    properties["CreationTime"] = ((DateTime)creationTime.TimeStamp1).ToString("O", CultureInfo.InvariantCulture);
                }
            }

            // Modified time
            var modifiedTime = fieldBoundary.TimeScopes.Where(ts => ts.DateContext == DateContextEnum.Modification).FirstOrDefault();

            properties.Add("ModifiedTime", null);
            if (modifiedTime != null)
            {
                if (modifiedTime.TimeStamp1 != null)
                {
                    properties["ModifiedTime"] = ((DateTime)modifiedTime.TimeStamp1).ToString("O", CultureInfo.InvariantCulture);
                }
            }

            Feature fieldBoundaryDto = new Feature(multiPolygon, properties);

            return(fieldBoundaryDto);
        }
        public Feature MapAsSingleFeature(DrivenHeadland drivenHeadlandAdapt, Feature fieldBoundary)
        {
            GeoJSON.Net.Geometry.MultiPolygon multiPolygon = MultiPolygonMapper.Map(drivenHeadlandAdapt.SpatialData, _properties.AffineTransformation);
            if (multiPolygon == null)
            {
                return(null);
            }

            Dictionary <string, object> properties = new Dictionary <string, object>();

            if (!String.IsNullOrEmpty(drivenHeadlandAdapt.Description))
            {
                properties.Add("HeadlandDescription", drivenHeadlandAdapt.Description);
            }
            foreach (var property in fieldBoundary.Properties)
            {
                properties.Add(property.Key, property.Value);
            }

            return(new Feature(multiPolygon, properties));
        }
        /// <summary>
        /// This method checks all coordinates of a collection if they're smaller or bigger then minima and maxima
        /// </summary>
        /// <param name="collection">the provided point collection</param>
        private void GetMinAndMaxValues(FeatureCollection collection)
        {
            for (var i = 0; i < collection.Features.Count(); i++)
            {
                GeoJSON.Net.Geometry.MultiPolygon geo = (GeoJSON.Net.Geometry.MultiPolygon)collection.Features[i].Geometry;

                // get all maxes and mins
                for (int o = 0; o < geo.Coordinates.Count(); o++)
                {
                    for (int p = 0; p < geo.Coordinates[o].Coordinates.Count(); p++)
                    {
                        for (int q = 0; q < geo.Coordinates[o].Coordinates[p].Coordinates.Count(); q++)
                        {
                            double x = this.ConvertX(geo.Coordinates[o].Coordinates[p].Coordinates[q].Longitude);
                            double y = this.ConvertY(geo.Coordinates[o].Coordinates[p].Coordinates[q].Latitude);

                            if (x <= this.xmin)
                            {
                                this.xmin = x;
                            }

                            if (x > this.xmax)
                            {
                                this.xmax = x;
                            }

                            if (y < this.ymin)
                            {
                                this.ymin = y;
                            }

                            if (y > this.ymax)
                            {
                                this.ymax = y;
                            }
                        }
                    }
                }
            }
        }
        public static AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon MapMultiPolygon(GeoJSON.Net.Geometry.MultiPolygon multiPolygonGeoJson, AffineTransformation affineTransformation = null)
        {
            var multiPolygon = new AgGateway.ADAPT.ApplicationDataModel.Shapes.MultiPolygon();

            foreach (var polygonGeoJson in multiPolygonGeoJson.Coordinates)
            {
                var polygon = PolygonMapper.MapPolygon(polygonGeoJson, affineTransformation);
                multiPolygon.Polygons.Add(polygon);
            }

            return(multiPolygon);
        }
        /// <summary>
        /// this method draws the map on the canvas
        /// </summary>
        /// <param name="path">The provided map</param>
        private void DrawMap(string path)
        {
            FeatureCollection geoCollection = GetData.GetFeatures(path);

            this.GetMinAndMaxValues(geoCollection);

            for (var i = 0; i < geoCollection.Features.Count(); i++)
            {
                string naam = geoCollection.Features[i].Properties["localname"].ToString();
                GeoJSON.Net.Geometry.MultiPolygon geo = (GeoJSON.Net.Geometry.MultiPolygon)geoCollection.Features[i].Geometry;
                this.colorCounter = i;
                Console.WriteLine("printing: " + naam);
                Console.WriteLine(this.colorCounter);
                for (int j = 0; j < geo.Coordinates.Count(); j++)
                {
                    for (int k = 0; k < geo.Coordinates[j].Coordinates.Count(); k++)
                    {
                        Polygon newPoly = new Polygon();
                        List <System.Windows.Point> pointList = new List <System.Windows.Point>();

                        // get all x and y points in a generic collection PointsList
                        for (int l = 0; l < geo.Coordinates[j].Coordinates[k].Coordinates.Count(); l++)
                        {
                            for (int q = 0; q < 1; q++)
                            {
                                double x = this.ConvertX(geo.Coordinates[j].Coordinates[k].Coordinates[l].Longitude);
                                double y = this.ConvertY(geo.Coordinates[j].Coordinates[k].Coordinates[l].Latitude);

                                pointList.Add(new System.Windows.Point(this.AddScalabilityX(x), this.AddScalabilityY(y)));
                            }
                        }

                        // point reduction
                        try
                        {
                            pointList = PointReduction.DouglasPeuckerReduction(pointList, double.Parse(Epsilon.Text));
                        }
                        catch
                        {
                            pointList = PointReduction.DouglasPeuckerReduction(pointList, 1.0);
                        }

                        List <PointF>   newPoints        = new List <PointF>();
                        PointCollection pointsCollection = new PointCollection();

                        for (int z = 0; z < pointList.Count(); z++)
                        {
                            newPoints.Add(new PointF((float)pointList[z].X, (float)pointList[z].Y));

                            // Console.WriteLine(PointList[z]);
                        }

                        // triangulation
                        // http://csharphelper.com/blog/2014/07/triangulate-a-polygon-in-c/
                        if (newPoints.ToArray().Length >= 3)
                        {
                            Polygon2        triPoly   = new Polygon2(newPoints.ToArray());
                            List <Triangle> triangles = triPoly.Triangulate();

                            foreach (Triangle tri in triangles)
                            {
                                newPoly          = new Polygon();
                                pointsCollection = new PointCollection();
                                for (int o = 0; o < tri.Points.Length; o++)
                                {
                                    pointsCollection.Add(new System.Windows.Point(tri.Points[o].X / 300, tri.Points[o].Y / 300));
                                }

                                // Console.WriteLine(pointsCollection.ToString());
                                this.Print3DTriangle(pointsCollection, this.colorCounter, this.arrayToUse);

                                // this.PrintPolygonTriangle(naam, pointsCollection, newPoly);
                            }

                            /*
                             * pointsCollection = new PointCollection();
                             *
                             * // Redraw the polygon.
                             * if (newPoints.Count >= 3)
                             * {
                             *  newPoly = new Polygon();
                             *  for (int o = 0; o < newPoints.Count; o++)
                             *  {
                             *      pointsCollection.Add(new System.Windows.Point(newPoints[o].X, newPoints[o].Y));
                             *  }
                             *
                             *  // Draw the polygon.
                             * // this.PrintPolygon(naam, pointsCollection, newPoly);
                             * }*/
                            GetNewColor();
                            this.colorCounter++;
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public static bool GetProjectionFromGeoJson(string fileName, out string projectionName, out string projection)
        {
            string       geoJson = File.ReadAllText(fileName);
            ProtoGeoJSON obj     = JsonConvert.DeserializeObject <ProtoGeoJSON>(geoJson);

            projectionName = string.Empty;
            projection     = string.Empty;
            ICRSObject crs = null;

            switch (obj.Type)
            {
            case "FeatureCollection":
                GFeatureCollection features = JsonConvert.DeserializeObject <GFeatureCollection>(geoJson);
                crs = features.CRS;
                break;

            case "Feature":
                GFeature feature = JsonConvert.DeserializeObject <GFeature>(geoJson);
                crs = feature.CRS;
                break;

            case "MultiPolygon":
                GMultiPolygon multiPoly = JsonConvert.DeserializeObject <GMultiPolygon>(geoJson);
                crs = multiPoly.CRS;
                break;

            case "Polygon":
                GPolygon poly = JsonConvert.DeserializeObject <GPolygon>(geoJson);
                crs = poly.CRS;
                break;

            default:
                break;
            }

            if (crs is NamedCRS)
            {
                NamedCRS nCrs = crs as NamedCRS;
                object   value;
                if (nCrs.Properties.TryGetValue("name", out value))
                {
                    string name = value as string;
                    if (name != null)
                    {
                        name = name.ToLower();
                        if (name.EndsWith("3857"))
                        {
                            projectionName = "EPSG:3857 Pseudo-Mercator";
                            projection     = CoordinateConverter.WebMercator.WKT;
                            return(true);
                        }
                        else
                        {
                            int    index      = name.IndexOf("epsg::");
                            string epsgStr    = name.Substring(index);
                            string authNumber = epsgStr.Substring(epsgStr.IndexOf("::") + 2);
                            projection = AutoProjection.GetProjection(authNumber);
                            if (projection != string.Empty)
                            {
                                projectionName = string.Format("EPSG:{0}", authNumber);
                                return(true);
                            }
                        }
                    }
                }
            }

            projectionName = "EPSG:4326 Lat-Long";
            projection     = CoordinateConverter.WGS84.WKT;
            return(true);
        }