//LocationCollection
        //public static bool IsLocationInComplexPolygon(List<Location> mainPolygon, List<List<Location>> holes, Location checkPoint)
        public static bool IsLocationInComplexPolygon(LocationCollection mainPolygon, List<LocationCollection> holes, Location checkPoint)
        {
            if (checkPoint != null)
            {
                // check if point is inside boundary box
                double minX = mainPolygon[0].Latitude;
                double maxX = mainPolygon[0].Latitude;
                double minY = mainPolygon[0].Longitude;
                double maxY = mainPolygon[0].Longitude;

                foreach (var q in mainPolygon)
                {
                    minX = Math.Min(q.Latitude, minX);
                    maxX = Math.Max(q.Latitude, maxX);
                    minY = Math.Min(q.Longitude, minY);
                    maxY = Math.Max(q.Longitude, maxY);
                }

                if (checkPoint.Latitude < minX || checkPoint.Latitude > maxX || checkPoint.Longitude < minY || checkPoint.Longitude > maxY)
                {
                    // point is not inside boundary box, do not continue
                    return false;
                }

                // check if point is inside main polygon
                var result = IsLocationInPolygon(mainPolygon, checkPoint);

                // point is not inside main polygon, do not continue
                if (result == false) return false;

                // check if point is not inside of any hole
                if (holes != null)
                {
                    foreach (var holePolygon in holes)
                    {
                        var holeResult = IsLocationInPolygon(holePolygon, checkPoint);

                        if (holeResult)
                        {
                            // point is inside hole, that means it doesn't belong to complex polygon, return false
                            return false;
                        }
                    }
                }

                // if all tests passed then point is inside Polygon.
                return true;

            }
            else
            {
                return false;
            }
        }
        //public static bool IsLocationInPolygon(List<Location> polygon, Location checkPoint)
        public static bool IsLocationInPolygon(LocationCollection polygon, Location checkPoint)
        {
            List<double> xList = new List<double>();
            List<double> yList = new List<double>();

            int sideCount = -1;
            foreach (var location in polygon)
            {
                sideCount++;
                xList.Add(location.Latitude);
                yList.Add(location.Longitude);
            }

            return pointInPolygon(sideCount, xList.ToArray(), yList.ToArray(), checkPoint.Latitude, checkPoint.Longitude);

        }
Beispiel #3
0
        public static List<MapPolyline> ReadPolylineData(string stringData, string LayerName)
        {
            List<MapPolyline> Polylines = new List<MapPolyline>();
            string aLine = null;
            int polyIterator = 0;
            var polydata = new { AttributeData = "", XData = "", YData = "" }; // sample
            var polyList = ListFactory.MakeList(polydata);
            string attrDat = null; string xDat = null; string yDat = null;


            StringReader strReader = new StringReader(stringData);
            while (true)
            {
                aLine = strReader.ReadLine();
                if (aLine != null)
                {
                    polyIterator++;
                    polyIterator = polyIterator > 3 ? 1 : polyIterator;
                    switch (polyIterator)
                    {
                        case 1:
                            attrDat = aLine;
                            break;
                        case 2:
                            xDat = aLine;
                            break;
                        case 3:
                            yDat = aLine;
                            polyList.Add(new { AttributeData = attrDat, XData = xDat, YData = yDat });
                            attrDat = null; xDat = null; yDat = null;
                            break;
                    }
                }
                else
                {
                    //process all the data
                    if (polyList.Count > 1)
                    {
                        foreach (var polyDatElem in polyList)
                        {
                            MapPolyline polyline = new MapPolyline();
                            polyline.Locations = new LocationCollection();

                            string[] attVals = polyDatElem.AttributeData.Split(','); // not used for now, use later for lineweight an annotation
                            string[] xVals = polyDatElem.XData.Split(',');
                            string[] yVals = polyDatElem.YData.Split(',');
                            if (xVals.Length == yVals.Length)
                            {
                                for (int i = 0; i < xVals.Length; i++)
                                {
                                    double x;
                                    double y;
                                    if (Double.TryParse(xVals[i], out x) && Double.TryParse(yVals[i], out y)) // if done, then is a number
                                    {
                                        Location thisLoc = new Location(x, y);
                                        polyline.Locations.Add(thisLoc);
                                    }
                                }
                               Polylines.Add(polyline);
                            }
                            else
                            {
                                throw new Exception("Number of X coordinates and Y coordinates in data does not match");
                            }
                        }
                    }
                    break;
                }
            }
            return Polylines;
        }
Beispiel #4
0
        private static LocationCollection GetLocationData(string coordinateStringData)
        {
            LocationCollection locations = new LocationCollection();
            string[] coordVals = coordinateStringData.Split(',');
            int numberOfCoordEntries = coordVals.Length;
            double Latitude, Longitude;

            if ( numberOfCoordEntries % 2 == 0 ) 
            {
                for (int i = 0; i < coordVals.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        Latitude = double.Parse(coordVals[i], CultureInfo.InvariantCulture);
                        Longitude = double.Parse(coordVals[i + 1], CultureInfo.InvariantCulture);
                        Location l = new Location(Latitude, Longitude);
                        locations.Add(l);
                    }
                }
            }
            return locations;
        }