public static void ReprojectFeatureDataSet(SharpMap.Data.FeatureDataSet fds, GeoAPI.CoordinateSystems.ICoordinateSystem target) { for (var i = 0; i < fds.Tables.Count; i++) { var fdt = fds.Tables[i]; ReprojectFeatureDataTable(fdt, target); } }
public static void ReprojectFeatureDataTable(SharpMap.Data.FeatureDataTable fdt, GeoAPI.CoordinateSystems.ICoordinateSystem target) { var source = SharpMap.CoordinateSystems.CoordinateSystemExtensions.GetCoordinateSystem(fdt[0].Geometry); var ctFactory = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var ct = ctFactory.CreateFromCoordinateSystems(source, target); var geomFactory = GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory((int)target.AuthorityCode); for (var i = 0; i < fdt.Rows.Count; i++) { var fdr = fdt[i]; fdr.Geometry = GeoAPI.CoordinateSystems.Transformations.GeometryTransform.TransformGeometry(fdr.Geometry, ct.MathTransform, geomFactory); } }
/// <summary> /// Reads and parses the projection if a projection file exists /// </summary> private void ParseProjection() { string projfile = Path.GetDirectoryName(Filename) + "\\" + Path.GetFileNameWithoutExtension(Filename) + ".prj"; if (System.IO.File.Exists(projfile)) { try { string wkt = System.IO.File.ReadAllText(projfile); _CoordinateSystem = (GeoAPI.CoordinateSystems.ICoordinateSystem)SharpMap.Converters.WellKnownText.CoordinateSystemWktReader.Parse(wkt); _CoordsysReadFromFile = true; } catch (System.Exception ex) { System.Diagnostics.Trace.TraceWarning("Coordinate system file '" + projfile + "' found, but could not be parsed. WKT parser returned:" + ex.Message); throw (ex); } } }
public override void Reproject(ProjectionInfo targetProjection) { var ctFac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var csFac = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var csTarget = csFac.CreateFromWkt(targetProjection.ToEsriString()); foreach (var layer in EnumerateLayers(_map)) { var lLayer = layer as SharpMap.Layers.Layer; if (lLayer == null) { continue; } GeoAPI.CoordinateSystems.ICoordinateSystem csSource = null; if (lLayer.CoordinateTransformation != null) { csSource = lLayer.CoordinateTransformation.SourceCS; } if (!string.IsNullOrEmpty(layer.Proj4Projection)) { csSource = csFac.CreateFromWkt(ProjectionInfo.FromProj4String(layer.Proj4Projection).ToEsriString()); } else if (layer.SRID != 0) { csSource = csFac.CreateFromWkt(ProjectionInfo.FromEpsgCode(layer.SRID).ToEsriString()); } var ctF = ctFac.CreateFromCoordinateSystems(csSource, csTarget); var ctR = ctFac.CreateFromCoordinateSystems(csTarget, csSource); lLayer.CoordinateTransformation = ctF; lLayer.ReverseCoordinateTransformation = ctR; } throw new InvalidOperationException("Cannot Setup CoordinateTransformation as long as ProjectionInfo does not maintain SRID values."); }
/// <summary> /// Reads and parses the projection if a projection file exists /// </summary> private void ParseProjection() { string projfile = Path.GetDirectoryName(Filename) + "\\" + Path.GetFileNameWithoutExtension(Filename) + ".prj"; if (System.IO.File.Exists(projfile)) { try { string wkt = System.IO.File.ReadAllText(projfile); _CoordinateSystem = (GeoAPI.CoordinateSystems.ICoordinateSystem)SharpMap.Converters.WellKnownText.CoordinateSystemWktReader.Parse(wkt); _CoordsysReadFromFile = true; } catch(System.Exception ex) { System.Diagnostics.Trace.TraceWarning("Coordinate system file '" + projfile + "' found, but could not be parsed. WKT parser returned:" + ex.Message); throw (ex); } } }