public static bool Transform(this DMesh3 dMesh, SpatialReference to) { string crs = dMesh.FindMetadata("CRS") as string; if (crs != null && crs != "") { SpatialReference from = new SpatialReference(null); if (crs.Contains("+proj")) { from.ImportFromProj4(crs); } else if (crs.StartsWith("epsg") || crs.StartsWith("EPSG")) { int epsg = int.Parse(crs.Split(':')[1]); from.ImportFromEPSG(epsg); } else { from.ImportFromWkt(ref crs); }; try { CoordinateTransformation trans = new CoordinateTransformation(from, to); for (int i = 0; i < dMesh.VertexCount; i++) { if (dMesh.IsVertex(i)) { Vector3d vertex = dMesh.GetVertex(i); double[] dV = new double[3] { vertex.x, vertex.y, vertex.z }; trans.TransformPoint(dV); AppState.instance.mapTrans.TransformPoint(dV); dMesh.SetVertex(i, new Vector3d(dV)); } } ; return(true); } catch { return(false); } } try { for (int i = 0; i < dMesh.VertexCount; i++) { if (dMesh.IsVertex(i)) { Vector3d vertex = dMesh.GetVertex(i); double[] dV = new double[3] { vertex.x, vertex.y, vertex.z }; AppState.instance.mapTrans.TransformPoint(dV); dMesh.SetVertex(i, new Vector3d(dV)); } } ; return(true); } catch { return(false); } }
static public Geometry ToGeometry(this LineString line) { Geometry geom = new Geometry(wkbGeometryType.wkbLineString); SpatialReference sr = new SpatialReference(null); ICRSObject crs = line.CRS; if (crs == null) { crs = new NamedCRS("EPSG:4326"); } switch (crs.Type) { case CRSType.Name: string name = (crs as NamedCRS).Properties["name"] as string; if (name.Contains("urn")) { sr.ImportFromUrl(name); } else if (name.Contains("EPSG")) { string[] args = name.Split(':'); sr.ImportFromEPSG(int.Parse(args[1])); } else { sr.SetWellKnownGeogCS(name); } break; case CRSType.Link: string url = (crs as LinkedCRS).Properties["href"] as string; sr.ImportFromUrl(url); break; case CRSType.Unspecified: sr.SetWellKnownGeogCS("EPSG:4326"); break; } geom.AssignSpatialReference(sr); Position[] vertexes = line.Points(); foreach (Position vertex in vertexes) { Nullable <double> alt = vertex.Altitude; geom.AddPoint(vertex.Latitude, vertex.Longitude, alt ?? 0.0); } return(geom); }
public static SpatialReference TextToSR(string str) { if (str.Contains("epsg:") || str.Contains("EPSG:")) { SpatialReference crs = new SpatialReference(null); string[] parts = str.Split(':'); crs.ImportFromEPSG(int.Parse(parts[1])); return(crs); } if (str.Contains("proj")) { SpatialReference crs = new SpatialReference(null); crs.ImportFromProj4(str); return(crs); } return(new SpatialReference(str)); }
public static bool ChkArgs(string[] args, ref string _in, ref OSGeo.OSR.SpatialReference _sr, ref string _out) { bool argOK = false; int check = 0; if (args.Length != 6) { return(false); } try { for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "-i": _in = args[i + 1]; check += 1; break; case "-cs": int.TryParse(args[i + 1], out int srid); _sr.ImportFromEPSG(srid); check += 10; break; case "-o": _out = args[i + 1]; if (Path.GetExtension(_out) != ".shp") { _out += ".shp"; } check += 100; break; } } if (check == 111) { argOK = true; } } catch { } return(argOK); }
/// <summary> /// Creates an OGR data source from a FeatureDataTable /// </summary> /// <param name="table">The name of the table</param> /// <param name="geometryType">The geometry type</param> /// <param name="driver">The driver</param> /// <param name="connection">The connection string</param> /// <param name="driverOptions">The options for the driver</param> /// <param name="layerOptions">The options for the layer</param> public static void CreateFromFeatureDataTable(FeatureDataTable table, OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null) { if (table == null) { throw new ArgumentNullException("table"); } if (table.Rows.Count == 0) { throw new ArgumentException("The table contains no rows", "table"); } if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon) { throw new ArgumentException("Invalid geometry type", "geometryType"); } if (string.IsNullOrWhiteSpace(driver)) { throw new ArgumentException("No driver specified", "driver"); } var dr = OSGeo.OGR.Ogr.GetDriverByName(driver); if (dr == null) { throw new Exception(string.Format("Cannot load driver '{0}'!", driver)); } //if (!dr.TestCapability("ODrCCreateDataSource")) // throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver)); // Create the data source var ds = dr.CreateDataSource(connection, driverOptions); //if (!ds.TestCapability("ODsCCreateLayer")) // throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver)); // Create the spatial reference var sr = new OSGeo.OSR.SpatialReference(string.Empty); sr.ImportFromEPSG(srid); // Create the layer var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions); sr.Dispose(); //lyr.GetSpatialRef(); foreach (System.Data.DataColumn dc in table.Columns) { using (var fldDef = GetFieldDefinition(dc)) lyr.CreateField(fldDef, 0); } using (var ld = lyr.GetLayerDefn()) { foreach (FeatureDataRow fdr in table.Rows) { if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType) { continue; } using (var feature = new OgrFeature(ld)) { feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary())); var idx = -1; foreach (System.Data.DataColumn dc in table.Columns) { idx++; var fd = ld.GetFieldDefn(idx); DateTime dt; switch (fd.GetFieldType()) { case OgrFieldType.OFTBinary: //Nothing break; case OgrFieldType.OFTDate: dt = ((DateTime)fdr[dc]).Date; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTDateTime: dt = (DateTime)fdr[dc]; feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case OgrFieldType.OFTTime: var tod = ((DateTime)fdr[dc]).TimeOfDay; feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0); break; case OgrFieldType.OFTInteger: feature.SetField(idx, Convert.ToInt32(fdr[dc])); break; case OgrFieldType.OFTIntegerList: var il = GetIntegerList(fdr[dc], dc.DataType); feature.SetFieldIntegerList(idx, il.Length, il); break; case OgrFieldType.OFTReal: feature.SetField(idx, Convert.ToDouble(fdr[dc])); break; case OgrFieldType.OFTRealList: var dl = GetDoubleList(fdr[dc], dc.DataType); feature.SetFieldDoubleList(idx, dl.Length, dl); break; case OgrFieldType.OFTString: feature.SetField(idx, Convert.ToString(fdr[dc])); break; case OgrFieldType.OFTStringList: var sl = (string[])fdr[dc]; feature.SetFieldStringList(idx, sl); break; } fd.Dispose(); } lyr.CreateFeature(feature); feature.Dispose(); } //ld.Dispose(); } } lyr.Dispose(); ds.Dispose(); dr.Dispose(); }
// public method to set envelope and transform to new projection /// <summary> /// Method to set <see cref="Envelope"/> and <see cref="Layer.CoordinateTransformation"/> to the projection of the map /// </summary> /// <param name="map">The map</param> public void ReprojectToMap(Map map) { ICoordinateSystem cs = null; if (map.SRID > 0) { using (var p = new OSGeo.OSR.SpatialReference(null)) { string wkt; p.ImportFromEPSG(map.SRID); p.ExportToWkt(out wkt); #if !DotSpatialProjections cs = new CoordinateSystemFactory().CreateFromWkt(wkt); #else cs = ProjectionInfo.FromEsriString(wkt); #endif } } ReprojectToCoordinateSystem(cs); }
private void filter_true() { string path = textBox_folder.Text; comboStart = dataGridView1.Rows[0].Cells[0].Value.ToString(); comboEnd = dataGridView1.Rows[0].Cells[1].Value.ToString(); string save_LM = "UFL_OPIP_LM" + "_" + comboStart + "_" + comboEnd; string save_PS = "UFL_OPIP_PS" + "_" + comboStart + "_" + comboEnd; string temp_LM = "UFL_OPIP_LM"; string temp_PS = "UFL_OPIP_PS"; string psFilePath = path + "\\" + save_PS; string lmFilePath = path + "\\" + save_LM; int dRcnt = dataGridView1.Rows.Count - 1; int dCcnt = dataGridView1.Columns.Count; textBox_status.AppendText("Shape파일을 생성합니다..." + "\r\n" + "\r\n"); Driver driver = Ogr.GetDriverByName("ESRI Shapefile"); DataSource data_source = driver.CreateDataSource(path, new string[] { "ENCODING=UTF-8" }); OSGeo.OSR.SpatialReference srs = new OSGeo.OSR.SpatialReference(""); srs.ImportFromEPSG(5186); // MessageBox.Show( dataGridView1.Rows[1].Cells[5].Value.ToString()); System.IO.FileInfo fi = new System.IO.FileInfo(psFilePath + ".shp"); if (fi.Exists) { MessageBox.Show("파일이 있습니다"); File.Delete(psFilePath + ".prj"); File.Delete(psFilePath + ".shp"); File.Delete(psFilePath + ".dbf"); File.Delete(psFilePath + ".shx"); } else { //Ps var layer = data_source.CreateLayer(temp_PS, srs, wkbGeometryType.wkbPoint, new string[] { "ENCODING=UTF-8" }); textBox_status.AppendText(psFilePath + ".shp 파일 생성" + "\r\n"); //string x_temp = dataGridView1.Rows[0].Cells[2].Value.ToString(); FieldDefn ftr_cde = new FieldDefn("FTR_CDE", FieldType.OFTString); FieldDefn hjd_cde = new FieldDefn("HJD_CDE", FieldType.OFTString); FieldDefn pip_dep = new FieldDefn("PIP_DEP", FieldType.OFTReal); FieldDefn start_point = new FieldDefn("시점", FieldType.OFTString); FieldDefn end_point = new FieldDefn("종점", FieldType.OFTString); FieldDefn field_x = new FieldDefn("X", FieldType.OFTReal); FieldDefn field_y = new FieldDefn("Y", FieldType.OFTReal); FieldDefn field_z = new FieldDefn("Z", FieldType.OFTReal); layer.CreateField(ftr_cde, 1); layer.CreateField(hjd_cde, 1); layer.CreateField(pip_dep, 1); layer.CreateField(start_point, 1); layer.CreateField(end_point, 1); layer.CreateField(field_x, 1); layer.CreateField(field_y, 1); layer.CreateField(field_z, 1); FeatureDefn ftr = layer.GetLayerDefn(); //FeatureDefn ftr = new FeatureDefn(null); ftr.SetGeomType(layer.GetLayerDefn().GetGeomType()); Feature ipFeature = new Feature(ftr); string wktPointZ = ""; Geometry ipGeom = null; for (int i = 0; i < dRcnt; ++i) { wktPointZ = String.Format("POINT Z({0} {1} {2})", dataGridView1.Rows[i].Cells[2].Value.ToString(), dataGridView1.Rows[i].Cells[3].Value.ToString(), dataGridView1.Rows[i].Cells[4].Value.ToString()); ipGeom = Ogr.CreateGeometryFromWkt(ref wktPointZ, srs); ipFeature.SetGeometry(ipGeom); ipFeature.SetField("FTR_CDE", "SF900"); ipFeature.SetField("HJD_CDE", ""); ipFeature.SetField("PIP_DEP", ""); ipFeature.SetField("시점", dataGridView1.Rows[i].Cells[0].Value.ToString()); ipFeature.SetField("종점", dataGridView1.Rows[i].Cells[1].Value.ToString()); ipFeature.SetField("X", dataGridView1.Rows[i].Cells[2].Value.ToString()); ipFeature.SetField("Y", dataGridView1.Rows[i].Cells[3].Value.ToString()); ipFeature.SetField("Z", dataGridView1.Rows[i].Cells[4].Value.ToString()); layer.CreateFeature(ipFeature); progressBar1.PerformStep(); } layer.CommitTransaction(); layer.SyncToDisk(); layer.Dispose(); data_source.Dispose(); System.IO.FileInfo fil = new System.IO.FileInfo(path + "\\" + temp_PS + ".shp"); if (fil.Exists) { System.IO.File.Move(path + "\\" + temp_PS + ".shp", psFilePath + ".shp"); System.IO.File.Move(path + "\\" + temp_PS + ".cpg", psFilePath + ".cpg"); System.IO.File.Move(path + "\\" + temp_PS + ".dbf", psFilePath + ".dbf"); System.IO.File.Move(path + "\\" + temp_PS + ".prj", psFilePath + ".prj"); System.IO.File.Move(path + "\\" + temp_PS + ".shx", psFilePath + ".shx"); } } //LM System.IO.FileInfo fLM = new System.IO.FileInfo(lmFilePath + ".shp"); if (fLM.Exists) { MessageBox.Show(lmFilePath + "이 있습니다"); File.Delete(lmFilePath + ".prj"); File.Delete(lmFilePath + ".shp"); File.Delete(lmFilePath + ".dbf"); File.Delete(lmFilePath + ".shx"); } else { //LM DataSource data_source2 = driver.CreateDataSource(path, new string[] { "ENCODING=UTF-8" }); var layer = data_source2.CreateLayer(temp_LM, srs, wkbGeometryType.wkbLineString, new string[] { "ENCODING=UTF-8" }); textBox_status.AppendText(lmFilePath + ".shp 파일 생성" + "\r\n"); FieldDefn ftr_cde = new FieldDefn("FTR_CDE", FieldType.OFTString); FieldDefn hjd_cde = new FieldDefn("HJD_CDE", FieldType.OFTString); FieldDefn pip_dep = new FieldDefn("PIP_DEP", FieldType.OFTReal); FieldDefn start_point = new FieldDefn("시점", FieldType.OFTString); FieldDefn end_point = new FieldDefn("종점", FieldType.OFTString); FieldDefn field_x = new FieldDefn("X", FieldType.OFTReal); FieldDefn field_y = new FieldDefn("Y", FieldType.OFTReal); FieldDefn field_z = new FieldDefn("Z", FieldType.OFTReal); layer.CreateField(ftr_cde, 1); layer.CreateField(hjd_cde, 1); layer.CreateField(pip_dep, 1); layer.CreateField(start_point, 1); layer.CreateField(end_point, 1); layer.CreateField(field_x, 1); layer.CreateField(field_y, 1); layer.CreateField(field_z, 1); FeatureDefn ftr = layer.GetLayerDefn(); //FeatureDefn ftr = new FeatureDefn(null); ftr.SetGeomType(layer.GetLayerDefn().GetGeomType()); Feature ipFeature = new Feature(ftr); Geometry ipGeom = null; string lineWKT = "LINESTRING ("; for (int i = 0; i < dRcnt; ++i) { string s_x = dataGridView1.Rows[i].Cells[2].Value.ToString(); string s_y = dataGridView1.Rows[i].Cells[3].Value.ToString(); if (i == dRcnt - 1) { lineWKT = lineWKT + s_x + " " + s_y + ")"; progressBar1.PerformStep(); } else { lineWKT = lineWKT + s_x + " " + s_y + ","; progressBar1.PerformStep(); } } ipGeom = Ogr.CreateGeometryFromWkt(ref lineWKT, srs); ipFeature.SetGeometry(ipGeom); ipFeature.SetField("FTR_CDE", "SF900"); ipFeature.SetField("HJD_CDE", ""); ipFeature.SetField("PIP_DEP", ""); ipFeature.SetField("시점", dataGridView1.Rows[0].Cells[0].Value.ToString()); ipFeature.SetField("종점", dataGridView1.Rows[0].Cells[1].Value.ToString()); layer.CreateFeature(ipFeature); layer.CommitTransaction(); layer.SyncToDisk(); layer.Dispose(); data_source2.Dispose(); System.IO.FileInfo film = new System.IO.FileInfo(path + "\\" + temp_LM + ".shp"); if (film.Exists) { System.IO.File.Move(path + "\\" + temp_LM + ".shp", lmFilePath + ".shp"); System.IO.File.Move(path + "\\" + temp_LM + ".cpg", lmFilePath + ".cpg"); System.IO.File.Move(path + "\\" + temp_LM + ".dbf", lmFilePath + ".dbf"); System.IO.File.Move(path + "\\" + temp_LM + ".prj", lmFilePath + ".prj"); System.IO.File.Move(path + "\\" + temp_LM + ".shx", lmFilePath + ".shx"); } textBox_status.AppendText("\r\n" + "Shape파일 생성이 완료되었습니다."); } }