Exemplo n.º 1
0
        static void TestCountryPolygonAPI()
        {
            // test polygon API

            string countryCode        = "NGA";
            double simplifyTolerlance = 0.0;

            GeoMultiPolygon fullNigeriaData = GeoAPI.ReadGeoPolygonByCountryKey(countryCode, simplifyTolerlance);

            if (fullNigeriaData == null)
            {
                Console.WriteLine("failed to get country data for specified country code: {0}", countryCode);
                return;
            }

            fullNigeriaData.saveToGeoJson("E:/Full_Nigeria.geojson");

            simplifyTolerlance = 0.02;

            GeoMultiPolygon simplifiedNigeriaDataPreserveTopology = GeoAPI.ReadGeoPolygonByCountryKey(countryCode, simplifyTolerlance);

            if (simplifiedNigeriaDataPreserveTopology == null)
            {
                Console.WriteLine("failed to get country data for specified country code: {0}", countryCode);
                return;
            }

            // access lat long data

            for (int i = 0; i < simplifiedNigeriaDataPreserveTopology.GetData().Count; i++)
            {
                GeoPolygon polygon = simplifiedNigeriaDataPreserveTopology.GetData()[i];

                for (int j = 0; j < polygon.GetData().Count; j++)
                {
                    GeoLineString lineString = polygon.GetData()[j];

                    for (int k = 0; k < lineString.GetData().Count; k++)
                    {
                        LonLat lonlat = lineString.GetData()[k];
                        Console.WriteLine("{0}d th x = {1}, y = {2}", i, lonlat.longitude, lonlat.latitude);
                    }
                }
            }

            // how to test result
            // we can save result to geojson file, open it in GIS tool(for example QGIS) and view it.

            bool saveSuccess = simplifiedNigeriaDataPreserveTopology.saveToGeoJson("E:/Simplified_Nigeria_0.02.geojson");

            if (!saveSuccess)
            {
                Console.WriteLine("failed to save");
            }
        }
Exemplo n.º 2
0
        public static GeoMultiPolygon ReadGeoPolygonByCountryKey(string iso3166_1CountryCode, double simplifyTolerlance, bool preserveTopology = true)
        {
            String sql = "SELECT geom FROM level0 WHERE GID_0 = " + "'" + iso3166_1CountryCode + "'";

            Layer resultLayer = _dataSource.ExecuteSQL(sql, null, "");

            if (resultLayer == null)
            {
                return(null);
            }

            if (resultLayer.GetFeatureCount(0) == 0)
            {
                return(null);
            }

            Feature  feature  = resultLayer.GetNextFeature();
            Geometry geometry = feature.GetGeometryRef();

            if (simplifyTolerlance > 0)
            {
                if (preserveTopology == true)
                {
                    geometry = geometry.SimplifyPreserveTopology(simplifyTolerlance);
                }
                else
                {
                    geometry = geometry.Simplify(simplifyTolerlance);
                }
            }

            int             geometryCount = geometry.GetGeometryCount();
            wkbGeometryType geomType      = geometry.GetGeometryType();

            string[] options      = new string[1];
            string   geometryJson = geometry.ExportToJson(options);

            GeoMultiPolygon multiPoygon = new GeoMultiPolygon();

            multiPoygon.SetData(geometry);
            multiPoygon.SetGeoJson(geometryJson);

            return(multiPoygon);
        }