Beispiel #1
0
 public WPM.Geom.Polygon GetGeomPolygon(PolygonPoint[] points)
 {
     WPM.Geom.Polygon poly    = new WPM.Geom.Polygon();
     WPM.Geom.Contour contour = new WPM.Geom.Contour();
     for (int k = 0; k < points.Length; k++)
     {
         contour.Add(new Point(points[k].X, points[k].Y));
     }
     poly.AddContour(contour);
     return(poly);
 }
Beispiel #2
0
        void ReplacePointsFromPolygon(Region region, WPM.Geom.Polygon polygon)
        {
            List <Point> points    = polygon.contours[0].points;
            int          numPoints = points.Count;

            PolygonPoint[] latlons = new PolygonPoint[numPoints];
            for (int k = 0; k < numPoints; k++)
            {
                latlons[k] = new PolygonPoint(points[k].x, points[k].y);
            }
            region.latlon = latlons;
            UpdatePointsFromLatLon(region);
        }
Beispiel #3
0
        void CountryMergeAdjacentRegions(Country targetCountry)
        {
            // Merges adjacent regions of target country
            int numRegions = targetCountry.regions.Count;

            WPM.Geom.Polygon[] countryPolys = new WPM.Geom.Polygon[numRegions];
            for (int k = 0; k < numRegions; k++)
            {
                countryPolys[k] = GetGeomPolygon(targetCountry.regions[k].latlon);
            }

            for (int k = 0; k < targetCountry.regions.Count; k++)
            {
                if (countryPolys[k] != null)
                {
                    Region region1 = targetCountry.regions[k];
                    for (int j = k + 1; j < targetCountry.regions.Count; j++)
                    {
                        if (countryPolys[j] != null)
                        {
                            PolygonClipper pc = new PolygonClipper(countryPolys[k], countryPolys[j]);
                            if (pc.OverlapsSubjectAndClipping())
                            {
                                pc.Compute(PolygonOp.UNION);
                                ReplacePointsFromPolygon(region1, pc.subject);
                                countryPolys[k] = GetGeomPolygon(targetCountry.regions[k].latlon);
                                targetCountry.regions.RemoveAt(j);
                                if (countryIndex >= 0 && targetCountry == _map.countries[countryIndex] && countryRegionIndex >= j)
                                {
                                    countryRegionIndex--;
                                }
                                countryPolys[j] = null;
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Changes province's owner to specified country
        /// </summary>
        public void ProvinceTransferTo()
        {
            if (provinceIndex < 0 || GUIProvinceTransferToCountryIndex < 0 || GUIProvinceTransferToCountryIndex >= countryNames.Length)
            {
                return;
            }

            // Get target country
            // recover GUI country index selection
            int targetCountryIndex = -1;

            string[] s = countryNames [GUIProvinceTransferToCountryIndex].Split(new char[] {
                '(',
                ')'
            }, System.StringSplitOptions.RemoveEmptyEntries);
            if (s.Length >= 2)
            {
                if (!int.TryParse(s [1], out targetCountryIndex))
                {
                    return;
                }
            }
            // Remove province form source country
            Province province      = map.provinces[provinceIndex];
            Country  sourceCountry = map.countries[countryIndex];

            if (map.countries[countryIndex].provinces != null)
            {
                List <Province> sourceProvinces = new List <Province>(sourceCountry.provinces);
                int             provIndex       = -1;
                for (int k = 0; k < sourceCountry.provinces.Length; k++)
                {
                    if (sourceCountry.provinces[k].name.Equals(province.name))
                    {
                        provIndex = k;
                    }
                }
                if (provIndex >= 0)
                {
                    sourceProvinces.RemoveAt(provIndex);
                    sourceCountry.provinces = sourceProvinces.ToArray();
                }
            }

            // Adds province to target country
            province.countryIndex = targetCountryIndex;
            Country         targetCountry = map.countries[targetCountryIndex];
            List <Province> destProvinces = new List <Province>(targetCountry.provinces);

            destProvinces.Add(province);
            targetCountry.provinces = destProvinces.ToArray();

            // Apply boolean operations on country polygons
            Region provinceRegion = province.regions[provinceRegionIndex];
            Region sourceRegion   = sourceCountry.regions[sourceCountry.mainRegionIndex];
            Region targetRegion   = targetCountry.regions[targetCountry.mainRegionIndex];

            // Extract from source country - only if province is in the frontier or is crossing the country
            RegionSanitize(sourceRegion);
            RegionSanitize(provinceRegion);
            WPM.Geom.Polygon provincePolygon = GetGeomPolygon(provinceRegion.latlon);
            PolygonClipper   pc = new PolygonClipper(GetGeomPolygon(sourceRegion.latlon), provincePolygon);

            if (pc.OverlapsSubjectAndClipping())
            {
                pc.Compute(PolygonOp.DIFFERENCE);
                ReplacePointsFromPolygon(sourceRegion, pc.subject);
            }

            // Add region to target country's polygon - only if the province is touching or crossing target country frontier
            RegionSanitize(targetRegion);
            pc = new PolygonClipper(GetGeomPolygon(targetRegion.latlon), provincePolygon);
            if (pc.OverlapsSubjectAndClipping())
            {
                pc.Compute(PolygonOp.UNION);
                ReplacePointsFromPolygon(targetRegion, pc.subject);
            }
            else
            {
                // Add new region to country
                Region newCountryRegion = new Region(targetCountry, targetCountry.regions.Count);
                newCountryRegion.points = new List <Vector3>(provinceRegion.points).ToArray();
                UpdateLatLonFromPoints(newCountryRegion);
                targetCountry.regions.Add(newCountryRegion);
            }

            // Finish operation
            map.HideCountryRegionHighlights(true);
            map.HideProvinceRegionHighlights(true);
            // Refresh definition of old country
            map.RefreshCountryDefinition(countryIndex, null);
            // Link province to target country and refresh definition of target country
            map.RefreshCountryDefinition(targetCountryIndex, null);
            map.RefreshProvinceDefinition(provinceIndex);
            countryChanges     = true;
            provinceChanges    = true;
            countryIndex       = targetCountryIndex;
            countryRegionIndex = targetCountry.mainRegionIndex;
            ProvinceRegionSelect();
        }