public void Reproject2280Test() { var sf = new Shapefile { GlobalCallback = this }; const string filename = @"Issues/MWGIS-91/utah_north_arcs.shp"; // In NAD83 / Utah North (ft), EPSG:2280 if (!sf.Open(filename)) { Assert.Fail("Could not open shapefile: " + sf.ErrorMsg[sf.LastErrorCode]); } Assert.IsTrue(sf.NumShapes == 1, "Unexpected number of shapes in " + filename); Console.WriteLine(sf.GeoProjection.ProjectionName); Helper.PrintExtents(sf.Extents); var proj = new GeoProjection(); proj.ImportFromEPSG(32612); // WGS 84 / UTM zone 12N var numShps = 0; var reprojectedSf = sf.Reproject(proj, ref numShps); Assert.IsTrue(numShps > 0, "Nothing is reprojected. Error: " + sf.ErrorMsg[sf.LastErrorCode]); Assert.IsNotNull(reprojectedSf, "reprojectedSf == null. Error: " + sf.ErrorMsg[sf.LastErrorCode]); Assert.AreEqual(sf.NumShapes, reprojectedSf.NumShapes); Helper.PrintExtents(reprojectedSf.Extents); Helper.SaveAsShapefile(reprojectedSf, Path.Combine(Path.GetTempPath(), "Reproject2280Test.shp")); Assert.AreNotEqual(Math.Round(sf.Extents.xMin, MidpointRounding.AwayFromZero), Math.Round(reprojectedSf.Extents.xMin, MidpointRounding.AwayFromZero), "xMin are the same, no projection has happened."); }
public void TransformToFourFigureGridReference() { // Source projection: var sourceProjection = new GeoProjection(); sourceProjection.ImportFromEPSG(4326); // WGS84 // Destination projection: var destProjection = new GeoProjection(); destProjection.ImportFromEPSG(27700); // OSGB 1936 / British National Grid // Source coordinates: var x = -2.02903211116781; var y = 53.4040442788744; Assert.IsTrue(sourceProjection.StartTransform(destProjection), "Cannot start transform"); sourceProjection.Transform(ref x, ref y); sourceProjection.StopTransform(); Assert.AreEqual(398167.22598, x, 0.01); Assert.AreEqual(389691.93091, y, 0.01); var gridReference = ConvertToFourFigureGridReference(x, y); Assert.AreEqual("SJ981896", gridReference); Debug.WriteLine("gridReference: " + gridReference); }
private void CreateShape(string path, DataTable dataTable) { Shapefile myShapefile; try { myShapefile = new Shapefile(); string fileName = Path.GetFileNameWithoutExtension(txtBox_excel.Text); string shapeilePath = Path.GetDirectoryName(path) + "\\" + fileName + ".shp"; Directory.CreateDirectory(Path.GetDirectoryName(shapeilePath)); myShapefile.CreateNew(shapeilePath, ShpfileType.SHP_POINT); //Create new field MapWinGIS.Field myField = new Field(); //Set the field properties myField.Name = "ID"; myField.Type = FieldType.INTEGER_FIELD; myField.Width = 10; //Add the filed for the shapefile table int intFieldIndex = 1; myShapefile.EditInsertField(myField, ref intFieldIndex, null); int myCounter = 0; int myShapeIndex = 0; //First Create header CreateHeader(dataTable.Columns, myShapefile); for (int i = 0; i < dataTable.Rows.Count; i++) { MapWinGIS.Shape myShape = new Shape(); myShape.Create(ShpfileType.SHP_POINT); MapWinGIS.Point myPoint = new MapWinGIS.Point(); myPoint.x = Convert.ToDouble(dataTable.Rows[i][x_Index]); myPoint.y = Convert.ToDouble(dataTable.Rows[i][y_Index]); object fld_Value = dataTable.Rows[i][0]; int myPointIndex = 0; myShape.InsertPoint(myPoint, ref myPointIndex); myShapefile.EditInsertShape(myShape, ref myShapeIndex); myShapefile.EditCellValue(0, myShapeIndex, fld_Value); CreatePointData(dataTable, myShapefile, myShapeIndex, i); myShapeIndex++; myCounter++; } GeoProjection proj = new GeoProjection(); // EPSG code proj.ImportFromEPSG(4326); // WGS84 myShapefile.GeoProjection = proj; myShapefile.StopEditingShapes(true, true, null); myShapefile.Close(); } catch (Exception) { throw; } }
private void MenuForm_Load(object sender, EventArgs e) { GeoProjection proj = new GeoProjection(); proj.ImportFromEPSG(3857); _shape = new Shapefile(); _shape.Open(@"E:\POSTE DE TRAVAIL\PROJETS PARTAGES\PROJET DE STAGE\USA_P\usa.shp"); _shape = _shape.Reproject(proj, 1); _idshape = axMap1.AddLayer(_shape, true); PointIco(@"E:\POSTE DE TRAVAIL\PROJETS PARTAGES\PROJET DE STAGE\ICON"); }
public void ProjectionStrings() { var utils = new Utils { GlobalCallback = this }; var gp = new GeoProjection { GlobalCallback = this }; // get NAD83 name var utilProjection = utils.GetNAD83ProjectionName(tkNad83Projection.Nad83_Alabama_East); gp.ImportFromEPSG((int)tkNad83Projection.Nad83_Alabama_East); var importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // get WGS84 name utilProjection = utils.GetWGS84ProjectionName(tkWgs84Projection.Wgs84_BLM_14N_ftUS); gp.ImportFromEPSG((int)tkWgs84Projection.Wgs84_BLM_14N_ftUS); importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // get NAD83 name by ID utilProjection = utils.GetProjectionNameByID((int)tkNad83Projection.Nad83_Alabama_East); gp.ImportFromEPSG((int)tkNad83Projection.Nad83_Alabama_East); importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // get WGS84 name by ID utilProjection = utils.GetProjectionNameByID((int)tkWgs84Projection.Wgs84_BLM_14N_ftUS); gp.ImportFromEPSG((int)tkWgs84Projection.Wgs84_BLM_14N_ftUS); importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // get obscure names by ID utilProjection = utils.GetProjectionNameByID(2402); gp.ImportFromEPSG(2402); importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // get obscure names by ID utilProjection = utils.GetProjectionNameByID(20005); gp.ImportFromEPSG(20005); importProjection = gp.Name; Assert.AreEqual(utilProjection, importProjection); // verify error utilProjection = utils.GetProjectionNameByID(100); Assert.IsTrue(utilProjection.Length == 0); // should return Index Out-of-bounds error var errorMsg = utils.ErrorMsg[utils.LastErrorCode]; Console.WriteLine(errorMsg); Assert.AreEqual("Index Out of Bounds", errorMsg); }
/// <summary> /// Shows information about selected projection /// </summary> /// <param name="projection"></param> public void ShowProjection(CoordinateSystem projection) { if (projection == null) { throw new NullReferenceException("Geoprojection wasn't passed"); } txtName.Text = projection.Name; txtCode.Text = projection.Code.ToString(); m_proj = new MapWinGIS.GeoProjection(); if (!m_proj.ImportFromEPSG(projection.Code)) { // usupported projection } else { projectionTextBox1.ShowProjection(m_proj.ExportToWKT()); projectionMap1.DrawCoordinateSystem(projection); projectionMap1.ZoomToCoordinateSystem(projection); txtProj4.Text = m_proj.ExportToProj4(); txtAreaName.Text = projection.AreaName; txtRemarks.Text = projection.Remarks; txtScope.Text = projection.Scope; } // showing dialects if (m_coordinateSystem != null) { m_database.ReadDialects(m_coordinateSystem); for (int i = 0; i < m_coordinateSystem.Dialects.Count; i++) { string s = m_coordinateSystem.Dialects[i]; ListViewItem item = this.listView1.Items.Add(i.ToString()); this.UpdateDialectString(item, s); } m_index = m_coordinateSystem.Dialects.Count; if (listView1.Items.Count > 0) { listView1.Items[0].Selected = true; } } }
// <summary> // Some operaions with GeoProjection object // </summary> public void GeoProjection(AxMap axMap1) { GeoProjection proj = new GeoProjection(); // EPSG code proj.ImportFromEPSG(4326); // WGS84 // proj 4 string proj.ImportFromProj4("+proj=longlat +datum=WGS84 +no_defs"); // WGS84 // autodetect the format string unknown_format = "4326"; proj.ImportFromAutoDetect(unknown_format); // from file string filename = "some_name"; proj.ReadFromFile(filename); // show the name of the loaded projection Debug.Print("Projection loaded: " + proj.Name); // show proj 4 representation Debug.Print("Proj4 representation: " + proj.ExportToProj4()); // let's show the properties of the geographic projection string s = ""; double[] arr = new double[5]; for (int i = 0; i < 5; i++) { // extract the parameter in element of val arr proj.get_GeogCSParam((tkGeogCSParameter)i, ref arr[i]); // append the name of parameter and the value to the string s += (tkGeogCSParameter)i + ": " + arr[i] + Environment.NewLine; } MessageBox.Show("Parameters of geographic coordinate system: " + Environment.NewLine + s); }
public static Shapefile CreateSfFromWkt(string wkt, int epsgCode) { var sf = new Shapefile(); if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON)) { throw new Exception("Can't create shapefile. Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var shp = new Shape(); if (!shp.ImportFromWKT(wkt)) { throw new Exception("Could not import wkt" + shp.ErrorMsg[shp.LastErrorCode]); } if (sf.EditAddShape(shp) == -1) { throw new Exception("Can't EditAddShape. Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var geoProjection = new GeoProjection(); if (!geoProjection.ImportFromEPSG(epsgCode)) { throw new Exception("Can't ImportFromEPSG Error: " + geoProjection.ErrorMsg[geoProjection.LastErrorCode]); } sf.GeoProjection = geoProjection; if (sf.HasInvalidShapes()) { throw new Exception("Shapefile has invalid shapes"); } return(sf); }
// Missing data [TestMethod, Timeout(5 * 60 * 1000)] public void ClipPolygon() { var settings = new GlobalSettings { OgrShareConnection = true }; const string folder = @"D:\dev\GIS-Data\Issues\ClipGridWithPolygon\"; // Create new in-memory shapefile: var sf = new Shapefile(); var retVal = sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON); Assert.IsTrue(retVal, "Could not CreateNewWithShapeID: " + sf.ErrorMsg[sf.LastErrorCode]); // Assign projection: var projection = new GeoProjection(); retVal = projection.ImportFromEPSG(4326); Assert.IsTrue(retVal, "Could not ImportFromEPSG(4326): " + projection.ErrorMsg[projection.LastErrorCode]); sf.GeoProjection = projection; // Create shape: var shp = new Shape(); retVal = shp.Create(ShpfileType.SHP_POLYGON); Assert.IsTrue(retVal, "Could not shp.Create: " + shp.ErrorMsg[shp.LastErrorCode]); // Add point of polygon: var numPoints = 0; shp.InsertPoint(new Point { y = 38.25853, x = 15.7033983 }, ref numPoints); shp.InsertPoint(new Point { y = 38.248108, x = 15.7033983 }, ref numPoints); shp.InsertPoint(new Point { y = 38.248108, x = 15.7245293 }, ref numPoints); shp.InsertPoint(new Point { y = 38.25853, x = 15.7245293 }, ref numPoints); // Make sure the polygon is closed by adding the first point as last: shp.InsertPoint(new Point { y = 38.25853, x = 15.7033983 }, ref numPoints); Assert.IsTrue(shp.IsValid, "Shape is invalid: " + shp.IsValidReason); // Add shape to shapefile: sf.EditAddShape(shp); // Save to file: Helper.SaveAsShapefile(sf, Path.Combine(folder, "ClippingArea-4326.shp")); // Clip grid, using Utils.ClipGridWithPolygon fails on the LandSat data, probably because it is in UInt16. var input = Path.Combine(folder, "LC08_L1TP_188033_20170919_20170920_01_RT_B4.TIF"); Assert.IsTrue(File.Exists(input), "Input file does not exists"); var output = Path.Combine(folder, "clipped.tif"); if (File.Exists(output)) { File.Delete(output); } var cutline = Path.Combine(folder, "ClippingArea-4326.shp"); Assert.IsTrue(File.Exists(cutline), "Cutline file does not exists"); var options = new[] { "-overwrite", "-crop_to_cutline", "-cutline", cutline }; retVal = _gdalUtils.GdalRasterWarp(input, output, options); Assert.IsTrue(retVal, "Could not ClipGridWithPolygon: " + _gdalUtils.ErrorMsg[_gdalUtils.LastErrorCode] + " Detailed error: " + _gdalUtils.DetailedErrorMsg); Assert.IsTrue(File.Exists(output), "Output file does not exists"); Debug.WriteLine(output); }
public bool ImportFromEpsg(int projCode) { _epsgCode = projCode; return(_projection.ImportFromEPSG(projCode)); }
private static Shapefile CreateBorder(double multiplier = 1d) { var sf = new Shapefile(); if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON)) { Assert.Fail("Can't create shapefile Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var geoProjection = new GeoProjection(); // WGS 84 / UTM zone 31N if (!geoProjection.ImportFromEPSG(32631)) { Assert.Fail("Can't ImportFromEPSG Error: " + geoProjection.ErrorMsg[geoProjection.LastErrorCode]); } sf.GeoProjection = geoProjection; const double startX = 693502.4; const double startY = 5841019.6; var shp = new Shape(); if (!shp.Create(ShpfileType.SHP_POLYGON)) { Assert.Fail("Can't create shape Error: " + shp.ErrorMsg[shp.LastErrorCode]); } var numPoints = 0; if (!shp.InsertPoint(new Point { x = startX, y = startY }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX - 107 * multiplier, y = startY - 12 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX - 9 * multiplier, y = startY - 99 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX + 11 * multiplier, y = startY - 83 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX, y = startY }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.IsValid) { Assert.Fail("Shape is invalid: " + shp.IsValidReason); } var numShapes = 0; if (!sf.EditInsertShape(shp, ref numShapes)) { Assert.Fail("Can't insert shape Error: " + sf.ErrorMsg[sf.LastErrorCode]); } Assert.IsFalse(sf.HasInvalidShapes(), "Created border file has invalid shapes"); return(sf); }