Exemplo n.º 1
0
        /// <summary>
        ///     The test file manager.
        /// </summary>
        private static void TestFileManager()
        {
            string sql    = "SELECT * FROM Buildings WHERE gid < 50";
            int    handle = Map.AddLayerFromDatabase(CONNECTION_STRING, sql, true);

            if (handle == -1)
            {
                Debug.Print("Failed to open layer: " + Map.FileManager.get_ErrorMsg(Map.FileManager.LastErrorCode));

                // in case the reason of failure is still unclear, let's ask GDAL for details
                var gs = new GlobalSettings();
                Debug.Print("Last GDAL error: " + gs.GdalLastErrorMsg);
            }
            else
            {
                OgrLayer l = Map.get_OgrLayer(handle);
                if (l != null)
                {
                    Debug.Print("Number of features: " + l.FeatureCount);

                    // now access the data
                    Shapefile sf = l.GetBuffer();
                    Debug.Print("Number of shapes: " + sf.NumShapes);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     The display underlying data.
        /// </summary>
        private static void DisplayUnderlyingData()
        {
            string layerName = "fields";

            var layer = new OgrLayer();

            if (layer.OpenFromDatabase(CONNECTION_STRING, layerName, false))
            {
                // let's frist try to extract some info without loading the data locally
                Extents ext = null;
                if (layer.get_Extents(out ext))
                {
                    Debug.WriteLine("Extents w/o loading the data: " + ext.ToDebugString());
                }

                Debug.WriteLine("Feature count: " + layer.FeatureCount);

                // now load the data locally and display the same info
                Shapefile shapefile = layer.GetBuffer();
                if (shapefile != null)
                {
                    Debug.WriteLine("Extents from the loaded data: " + shapefile.Extents.ToDebugString());

                    Debug.WriteLine("Shape count: " + shapefile.NumShapes);

                    Debug.WriteLine("Layer fields: ");
                    for (int i = 0; i < shapefile.NumFields; i++)
                    {
                        Debug.WriteLine(shapefile.get_Field(i).Name);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void ReadAttributesFromPostGISLayer()
        {
            var ogrDatasource = new OgrDatasource();

            try
            {
                var result = ogrDatasource.Open("PG:host=127.0.0.1 port=5432 dbname=mw_test user=mapwindow password=test123");
                Assert.IsTrue(result, "Cannot open PostGIS Connectie: " + ogrDatasource.GdalLastErrorMsg);
                Assert.IsTrue(ogrDatasource.LayerCount > 1, "No layers found");

                OgrLayer attributeLayer = null;
                for (var i = 0; i < ogrDatasource.LayerCount; i++)
                {
                    var layer = ogrDatasource.GetLayer(i);
                    Console.WriteLine(layer.Name);
                    if (layer.Name == "attributes")
                    {
                        attributeLayer = layer;
                    }
                }

                Assert.IsNotNull(attributeLayer, "Couldn't find the attribute layer");
                Console.WriteLine("Working with attributes layer");

                var sf = attributeLayer.GetBuffer();
                Assert.IsNotNull(sf, "Could not get buffer");

                var numShapes = sf.NumShapes;
                Assert.AreEqual(3, numShapes);

                var numFields = sf.NumFields;
                Assert.IsTrue(numFields > 0, "No fields found");

                var fidColumn = attributeLayer.FIDColumnName;
                Console.WriteLine("fidColumn: " + fidColumn);

                // First shape has all attributes filled:
                Console.WriteLine("Testing first shape, all filled");
                for (var fieldIndex = 0; fieldIndex < numFields; fieldIndex++)
                {
                    var value = sf.CellValue[fieldIndex, 0];
                    Console.WriteLine($"{sf.Field[fieldIndex].Name}: {value}");
                    Assert.IsNotNull(value, $"{sf.Field[fieldIndex].Name} should not be null");
                }

                // Second shape has only the geometry filled and the rest default values (which should be NULL):
                Console.WriteLine("Testing second shape, default values");
                var numNonNulls = 0;
                for (var fieldIndex = 0; fieldIndex < numFields; fieldIndex++)
                {
                    var value = sf.CellValue[fieldIndex, 1];
                    Console.WriteLine($"{sf.Field[fieldIndex].Name}: {value}");
                    // Skip FID because it always has a value:
                    if (sf.Field[fieldIndex].Name == fidColumn)
                    {
                        continue;
                    }
                    if (value != null)
                    {
                        numNonNulls++;
                    }
                }
                if (numNonNulls > 0)
                {
                    // No assert, need to check the third shape as well:
                    Console.WriteLine($"Error! Second shape has {numNonNulls} non NULL fields!");
                }

                // Third shape has all fields explicitly NULL:
                Console.WriteLine("Testing third shape, all null");
                numNonNulls = 0;
                for (var fieldIndex = 0; fieldIndex < numFields; fieldIndex++)
                {
                    var value = sf.CellValue[fieldIndex, 2];
                    Console.WriteLine($"{sf.Field[fieldIndex].Name}: {value}");
                    // Skip FID because it always has a value:
                    if (sf.Field[fieldIndex].Name == fidColumn)
                    {
                        continue;
                    }
                    if (value != null)
                    {
                        numNonNulls++;
                    }
                }
                Assert.AreEqual(0, numNonNulls, $"Third shape has {numNonNulls} non NULL fields!");
            }
            finally
            {
                ogrDatasource.Close();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///     The test edit layer.
        /// </summary>
        /// <returns>
        ///     The <see cref="bool" />.
        /// </returns>
        private static bool TestEditLayer()
        {
            var layer = new OgrLayer();

            layer.GlobalCallback = form;

            if (!layer.OpenFromDatabase(CONNECTION_STRING, "buildings", true))
            {
                Debug.Print("Failed to open layer: " + layer.get_ErrorMsg(layer.LastErrorCode));
                return(false);
            }

            // check if editing is supported for driver
            Debug.Print("Driver supports editing: " + layer.TestCapability(tkOgrLayerCapability.olcRandomWrite));

            // now check if we can actually do it, as there can be other limitations
            if (!layer.get_SupportsEditing(tkOgrSaveType.ostSaveAll))
            {
                Debug.Print("Can't edit a layer: " + layer.get_ErrorMsg(layer.LastErrorCode));

                layer.Close();
                return(false);
            }

            Shapefile sf = layer.GetBuffer();

            if (sf != null)
            {
                // possible types of editing
                bool editValue   = true;
                bool addShape    = true;
                bool editShape   = true;
                bool removeShape = true;

                if (editValue)
                {
                    int    shapeIndex = 0;
                    int    fieldIndex = 2;
                    object val        = sf.get_CellValue(fieldIndex, shapeIndex);
                    sf.EditCellValue(fieldIndex, shapeIndex, "test_writing");

                    // this flag will notify the driver that changes should saved back to source
                    sf.ShapeModified[shapeIndex] = true;
                }

                if (addShape)
                {
                    int   shapeIndex = sf.NumShapes;
                    Shape shp        = sf.get_Shape(0);
                    shp = shp.Buffer(1, 50);

                    // modified flag is set automatically in this case
                    bool result = sf.EditInsertShape(shp, ref shapeIndex);
                    Debug.Print("Shape was inserted: " + result);
                }

                if (editShape)
                {
                    // since shapefile is in in-memory mode, geometry of shapes can be changed directly;
                    // bear in mind that this won't work for file-based shapefiles, in that case get_Shape will
                    // populate Shape object which will have no futher link with parent shapefile
                    Shape shp = sf.get_Shape(sf.NumShapes - 1);
                    for (int i = 0; i < shp.numPoints; i++)
                    {
                        double x = 0.0, y = 0.0;
                        if (shp.get_XY(i, ref x, ref y))
                        {
                            shp.put_XY(i, x + 0.01, y + 0.01); // let's move it a little
                        }
                    }
                }

                if (removeShape)
                {
                    bool result = sf.EditDeleteShape(sf.NumShapes - 1);
                    Debug.Print("Shape was deleted: " + result);
                }

                // saving it
                int             count;
                tkOgrSaveResult saveResults = layer.SaveChanges(out count);

                Debug.Print("Save result: " + saveResults);
                Debug.Print("Number of shapes saved: " + count);

                // displaying info on errors
                for (int i = 0; i < layer.UpdateSourceErrorCount; i++)
                {
                    Debug.Print(
                        "Error for shape id {0}: {1}",
                        layer.UpdateSourceErrorShapeIndex[i],
                        layer.UpdateSourceErrorMsg[i]);
                }

                return(true);
            }

            layer.Close();
            return(false);
        }