private void PopulateList() { if (_datasource == null) { return; } listView1.Items.Clear(); for (int i = 0; i < _datasource.LayerCount; i++) { var layer = _datasource.GetLayer(i); if (layer == null) { continue; } var item = listView1.Items.Add(layer.Name); item.SubItems.Add(layer.FeatureCount.ToString()); item.SubItems.Add(layer.ShapeType.ToString()); var gp = layer.GeoProjection; int epsg; string srid = gp.TryAutoDetectEpsg(out epsg) ? epsg.ToString() : gp.ExportToProj4(); item.SubItems.Add(srid); } }
public void CreateLayerSQLiteTest() { var ogrDatasource = new OgrDatasource(); try { var result = ogrDatasource.Open2(@"sqlite\onepoint.sqlite", true); Assert.IsTrue(result, "Cannot open SQLite file: " + ogrDatasource.GdalLastErrorMsg); var settings = new GlobalSettings { OgrLayerForceUpdateMode = true }; var capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcCreateLayer); Debug.WriteLine("odcCreateLayer: " + capability); Assert.IsTrue(capability, "Cannot create layer"); var originalLayerCount = ogrDatasource.LayerCount; var projection = new GeoProjection(); Assert.IsTrue(projection.SetWgs84(), "Cannot set projection"); var layerCreated = ogrDatasource.CreateLayer("Test", ShpfileType.SHP_POINT, projection, "OVERWRITE=YES"); Assert.IsTrue(layerCreated, "Cannot create layer"); Debug.WriteLine(ogrDatasource.GdalLastErrorMsg); Assert.AreEqual(originalLayerCount + 1, ogrDatasource.LayerCount, "New layer isn't created"); Debug.WriteLine("GetLayerName: " + ogrDatasource.GetLayerName(ogrDatasource.LayerCount - 1)); var firstLayer = ogrDatasource.GetLayer(0); Assert.IsNotNull(firstLayer, $"Could not get first layer: {ogrDatasource.GdalLastErrorMsg}"); // Get layer: var newLayer = ogrDatasource.GetLayer(ogrDatasource.LayerCount - 1, true); // var newLayer = ogrDatasource.GetLayerByName("test", true); Assert.IsNotNull(newLayer, $"Could not get new layer: {ogrDatasource.GdalLastErrorMsg}"); // Add field: var numFeatures = newLayer.FeatureCount[true]; Debug.WriteLine("numFeatures: " + numFeatures); TestSQLiteLayers(ogrDatasource); } finally { if (ogrDatasource.LayerCount > 1) { ogrDatasource.DeleteLayer(ogrDatasource.LayerCount); } ogrDatasource.Close(); } }
private static void ListLayers() { var ds = new OgrDatasource(); if (!ds.Open(CONNECTION_STRING)) { Debug.Print("Failed to establish connection: " + ds.GdalLastErrorMsg); } else { int count = ds.LayerCount; Debug.Print("Number of layers: " + count); Debug.Print("List of layers by name:"); for (int i = 0; i < count; i++) { var lyr = ds.GetLayer(i); Debug.Print("Layer name: " + lyr.Name); Debug.Print("Projection: " + lyr.GeoProjection.ExportToProj4()); Debug.Print("Shape type: " + lyr.ShapeType); lyr.Close(); } ds.Close(); } }
public void ReadSchemaNamesFromPostGIS() { 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"); var stopwatch = new Stopwatch(); stopwatch.Start(); Console.WriteLine("Using GetLayer:"); for (var i = 0; i < ogrDatasource.LayerCount; i++) { var layer = ogrDatasource.GetLayer(i); Console.WriteLine(layer.Name); } stopwatch.Stop(); Console.WriteLine("GetLayer took " + stopwatch.Elapsed); // TODO Get layername with schema name Assert.Fail("TODO"); } finally { ogrDatasource.Close(); } }
public void OpenPostGISDifferentPortTest() { var ogrDatasource = new OgrDatasource(); try { var result = ogrDatasource.Open("PG:host=127.0.0.1 port=55432 dbname=aw_croppingscheme user=aw_croppingschem password=test123"); Assert.IsTrue(result, "Cannot open PostGIS Connectie: " + ogrDatasource.GdalLastErrorMsg); var settings = new GlobalSettings(); var capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcCreateLayer); Console.WriteLine("odcCreateLayer: " + capability); Assert.IsTrue(capability, "Cannot create layer"); capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcDeleteLayer); Console.WriteLine("odcDeleteLayer: " + capability); Assert.IsTrue(capability, "Cannot delete layer"); capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcCreateDataSource); Console.WriteLine("odcCreateDataSource: " + capability); //Assert.IsTrue(capability), "Cannot create datasource"); capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcDeleteDataSource); Console.WriteLine("odcDeleteDataSource: " + capability); //Assert.IsTrue(capability, "Cannot delete datasource"); capability = ogrDatasource.TestCapability(tkOgrDSCapability.odcCreateGeomFieldAfterCreateLayer); Console.WriteLine("odcCreateGeomFieldAfterCreateLayer: " + capability); Assert.IsTrue(capability, "Cannot create GeomField After CreateLayer"); Assert.IsTrue(ogrDatasource.LayerCount > 1, "No layers found"); for (var i = 0; i < ogrDatasource.LayerCount; i++) { var layer = ogrDatasource.GetLayer(i); Console.WriteLine(layer.Name); } } finally { ogrDatasource.Close(); } }
private static bool DisplayAllLayers() { var ds = new OgrDatasource(); if (!ds.Open(CONNECTION_STRING)) { Debug.WriteLine("Failed to establish connection: " + ds.GdalLastErrorMsg); } else { map.RemoveAllLayers(); // make sure it matches SRID of the layers (4326 in this case) map.Projection = tkMapProjection.PROJECTION_WGS84; for (int i = 0; i < ds.LayerCount; i++) { var layer = ds.GetLayer(i); if (layer != null) { int handle = map.AddLayer(layer, true); if (handle == -1) { Debug.WriteLine("Failed to add layer to the map: " + map.get_ErrorMsg(map.LastErrorCode)); } else { Debug.WriteLine("Layer was added the map: " + layer.Name); } } } map.ZoomToMaxVisibleExtents(); ds.Close(); } return(true); }
public void ShapefileDataTest() { var tempFolder = Path.GetTempPath(); var tempFilename = Path.Combine(tempFolder, "ShapefileDataTest.shp"); var esriShapefilePath = @"C:\dev\MapWinGIS\unittests\MapWinGISTests\Testdata\Issues\MWGIS-48-68\EsriShapefile.shp"; Helper.DeleteShapefile(tempFilename); bool result; // Create shapefile var sf = new Shapefile { GlobalCallback = this }; try { result = sf.CreateNewWithShapeID(tempFilename, ShpfileType.SHP_POINT); Assert.IsTrue(result, "Could not create shapefile"); Assert.IsTrue(sf.EditingShapes, "Shapefile is not in edit shapes mode"); Assert.IsTrue(sf.EditingTable, "Shapefile is not in edit table mode"); // Add fields of each data type: var fieldIndex = sf.EditAddField("intField", FieldType.INTEGER_FIELD, 0, 10); Assert.AreEqual(1, fieldIndex, "Could not add Integer field"); var width = sf.Field[fieldIndex].Width; Assert.AreEqual(9, width, "Integer field did not shrink to 9 digits"); fieldIndex = sf.EditAddField("dateField", FieldType.DATE_FIELD, 0, 6); Assert.AreEqual(2, fieldIndex, "Could not add Date field"); width = sf.Field[fieldIndex].Width; Assert.AreEqual(8, width, "Date field did not expand to 8 digits"); fieldIndex = sf.EditAddField("boolField", FieldType.BOOLEAN_FIELD, 0, 3); Assert.AreEqual(3, fieldIndex, "Could not add Boolean field"); width = sf.Field[fieldIndex].Width; Assert.AreEqual(1, width, "Boolean field did not shrink to 1 character"); // Assert.AreEqual(fieldIndex + 1, sf.NumFields, "Number of fields are incorrect"); result = sf.Save(); Assert.IsTrue(result, "Could not save shapefile"); Assert.AreEqual(fieldIndex + 1, sf.NumFields, "Number of fields are incorrect"); // Create shape: var shp = new Shape(); result = shp.Create(ShpfileType.SHP_POINT); Assert.IsTrue(result, "Could not create point shape"); var idx = sf.EditAddShape(shp); // Add data: result = sf.EditCellValue(sf.FieldIndexByName["intField"], idx, 99); Assert.IsTrue(result, "Could not edit intField"); DateTime dt = System.DateTime.Now; result = sf.EditCellValue(sf.FieldIndexByName["dateField"], idx, dt); Assert.IsTrue(result, "Could not edit dateField"); result = sf.EditCellValue(sf.FieldIndexByName["boolField"], idx, true); Assert.IsTrue(result, "Could not edit boolField"); result = sf.StopEditingShapes(); Assert.IsTrue(result, "Could not stop editing shapefile"); // Read back data: for (idx = 0; idx < sf.NumShapes; idx++) { int iField = (int)sf.CellValue[sf.FieldIndexByName["intField"], idx]; Assert.AreEqual(iField, 99, "intField value of 99 was not returned"); DateTime dField = (DateTime)sf.CellValue[sf.FieldIndexByName["dateField"], idx]; Assert.IsTrue(DateTime.Now.DayOfYear.Equals(((DateTime)dField).DayOfYear), "dateField value of Now was not returned"); bool bField = (bool)sf.CellValue[sf.FieldIndexByName["boolField"], idx]; Assert.AreEqual(bField, true, "boolField value of True was not returned"); } } finally { // Close the shapefile: result = sf.Close(); Assert.IsTrue(result, "Could not close shapefile"); } // although the default setting, indicate intent to interpret Y/N OGR String fields as Boolean GlobalSettings gs = new GlobalSettings(); gs.OgrInterpretYNStringAsBoolean = true; // setting to false results in exception reading boolField below // open as OGRLayer OgrDatasource _datasource = new OgrDatasource(); _datasource.GlobalCallback = this; if (_datasource.Open(tempFilename)) // "ESRI Shapefile:" + { // read layer through OGR library IOgrLayer ogrLayer = _datasource.GetLayer(0); sf = ogrLayer.GetBuffer(); for (int idx = 0; idx < sf.NumShapes; idx++) { int iField = (int)sf.CellValue[sf.FieldIndexByName["intField"], idx]; Assert.AreEqual(iField, 99, "intField value of 99 was not returned"); DateTime dField = (DateTime)sf.CellValue[sf.FieldIndexByName["dateField"], idx]; Assert.IsTrue(DateTime.Now.DayOfYear.Equals(((DateTime)dField).DayOfYear), "dateField value of Now was not returned"); bool bField = (bool)sf.CellValue[sf.FieldIndexByName["boolField"], idx]; Assert.AreEqual(bField, true, "boolField value of True was not returned"); } sf.Close(); } // open and read a Shapefile created by ESRI MapObjects, including a Boolean and Date field // table has a Boolean 'Inspected' field, and a Date 'InspDate' field Assert.IsTrue(sf.Open(esriShapefilePath, this)); for (int fld = 0; fld < sf.NumFields; fld++) { Console.WriteLine(string.Format("Field({0}): Name = '{1}', Fieldtype = {2}", fld, sf.Field[fld].Name, sf.Field[fld].Type)); } for (int idx = 0; idx < sf.NumShapes; idx++) { // read 'Inspected' value as object object inspected = sf.CellValue[sf.FieldIndexByName["Inspected"], idx]; // verify that it's a bool Assert.IsTrue(inspected is bool); // watch for Inspected rows (there aren't many) if ((bool)inspected == true) { // read 'InspDate' value as object object dt = sf.CellValue[sf.FieldIndexByName["InspDate"], idx]; // verify that it's a Date Assert.IsTrue(dt is DateTime); Console.WriteLine(string.Format("idx = {0}, Inspected = true, Inspection Date = {1}", idx, (DateTime)dt)); } } sf.Close(); }
public VectorLayer GetLayer(int index, bool forUpdate = false) { var layer = _datasource.GetLayer(index, forUpdate); return(layer != null ? new VectorLayer(layer) : null); }
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(); } }