示例#1
0
        private void TestLabelSerialize()
        {
            // As mentioned here https://github.com/MapWindow/MapWinGIS/issues/1
            var sf = new ShapefileClass();

            if (!sf.Open(@"D:\dev\GIS-Data\Issues\0001\LOT_N.shp"))
            {
                Debug.WriteLine(DateTime.Now + " Could not open shapefile. Reason " + sf.ErrorMsg[sf.LastErrorCode]);
                return;
            }

            if (sf.Labels.Count == 0)
            {
                Debug.WriteLine("Create labels");
                sf.GenerateLabels(0, tkLabelPositioning.lpCenter, true);
            }

            Debug.WriteLine(sf.Labels.Serialize());

            // Add shapefile to map:
            var hndl = axMap1.AddLayer(sf, true);

            var sf2 = axMap1.get_Shapefile(hndl);

            txtResults.Text = sf2.Labels.Serialize();
        }
示例#2
0
        private void button4_Click(object sender, EventArgs e)
        {
            _settings.OgrStringEncoding = tkOgrEncoding.oseUtf8;

            var sf = new ShapefileClass();

            if (!sf.Open(@"D:\dev\GIS-Data\Issues\Persian\roads.shp"))
            {
                Debug.WriteLine(DateTime.Now + " Could not open shapefile. Reason " + sf.ErrorMsg[sf.LastErrorCode]);
                return;
            }

            if (sf.Labels.Count == 0)
            {
                Debug.WriteLine("Create labels");
                sf.GenerateLabels(1, tkLabelPositioning.lpLongestSegement, true);
            }

            // Add shapefile to map:
            var hndl = axMap1.AddLayer(sf, true);

            txtResults.Text += @"Added layer with handle " + hndl;
        }
示例#3
0
        private static void GridStatistics()
        {
            const string workingFolder  = @"D:\dev\GIS-Data\Siebe\NetCDF\";
            var          currentProcess = Process.GetCurrentProcess();

            using (var writer = new StreamWriter(Path.Combine(workingFolder, "GridStatisticsForPolygon.log"), false))
            {
                currentProcess.Refresh();
                writer.WriteLine(DateTime.Now + " Starting. Total memory: " + FormatBytes(currentProcess.WorkingSet64));
                // Load shapefile with polygons:
                var sf = new ShapefileClass();
                if (!sf.Open(Path.Combine(workingFolder, "ARK.shp")))
                {
                    writer.WriteLine(
                        DateTime.Now + " Could not open shapefile. Reason " + sf.ErrorMsg[sf.LastErrorCode]);
                    return;
                }

                var numShapes = sf.NumShapes;

                var netCdfFiles = Directory.GetFiles(workingFolder, "*.nc");
                var utils       = new UtilsClass();
                _settings.ResetGdalError();
                var amersfoort = new GeoProjectionClass();
                amersfoort.ImportFromEPSG(28992);

                // Check projection:
                if (!sf.GeoProjection.IsSame[amersfoort])
                {
                    sf.GeoProjection = amersfoort.Clone();
                }

                // Loop through all netCDF files in folder:
                foreach (var netCdfFile in netCdfFiles)
                {
                    var grd = new GridClass();
                    if (!grd.Open(netCdfFile))
                    {
                        writer.WriteLine(DateTime.Now + " Could not open {0}. Reason: {1}, GDAL Error: {2}", netCdfFile,
                                         grd.ErrorMsg[grd.LastErrorCode], _settings.GdalLastErrorMsg);
                        continue;
                    }

                    currentProcess.Refresh();
                    writer.WriteLine(DateTime.Now + "Loaded grid " + netCdfFile + " Current memory " +
                                     FormatBytes(currentProcess.WorkingSet64));

                    var header      = grd.Header;
                    var noDataValue = (double)grd.Header.NodataValue;
                    var extents     = grd.Extents;

                    // Set projection:
                    header.GeoProjection = amersfoort.Clone();

                    // Get each band:
                    var numBands = grd.NumBands;
                    for (var i = 0; i < numBands; i++)
                    {
                        if (!grd.OpenBand(i))
                        {
                            writer.WriteLine(DateTime.Now + " Could not open band {0}. Reason: {1}, GDAL Error: {2}", i,
                                             grd.ErrorMsg[grd.LastErrorCode], _settings.GdalLastErrorMsg);
                            continue;
                        }

                        currentProcess.Refresh();
                        writer.WriteLine(DateTime.Now + " Working with band {0}  Current memory: {1} ", i,
                                         FormatBytes(currentProcess.WorkingSet64));
                        var mean = 0d;
                        var min  = 0d;
                        var max  = 0d;

                        for (var j = 0; j < numShapes; j++)
                        {
                            var shp = sf.Shape[j];
                            if (
                                !utils.GridStatisticsForPolygon(grd, header, extents, shp, noDataValue, ref mean,
                                                                ref min,
                                                                ref max))
                            {
                                writer.WriteLine(DateTime.Now + " Error getting statistics: " +
                                                 utils.ErrorMsg[utils.LastErrorCode]);
                            }

                            currentProcess.Refresh();
                            writer.WriteLine(DateTime.Now + " Mean: {0}, Min: {1}, Max: {2}, Current memory: {3}", mean,
                                             min, max, FormatBytes(currentProcess.WorkingSet64));
                        }
                    }

                    // Close grid:
                    grd.Close();
                    currentProcess.Refresh();
                    writer.WriteLine(DateTime.Now + " Closed grid Current memory: " +
                                     FormatBytes(currentProcess.WorkingSet64));
                }

                currentProcess.Refresh();
                writer.WriteLine(DateTime.Now + " Done. Current memory: " + FormatBytes(currentProcess.WorkingSet64));
            }

            Debug.WriteLine("Ready");
        }