private void AddShapeLayer_OnClick(object sender, RoutedEventArgs e) { var ofd = new OpenFileDialog(); ofd.Filter = "Shapefiles (*.shp)|*.shp"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var ds = new SharpMap.Data.Providers.ShapeFile(ofd.FileName); var lay = new SharpMap.Layers.VectorLayer(System.IO.Path.GetFileNameWithoutExtension(ofd.FileName), ds); if (ds.CoordinateSystem != null) { GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformationFactory fact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); lay.CoordinateTransformation = fact.CreateFromCoordinateSystems(ds.CoordinateSystem, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); lay.ReverseCoordinateTransformation = fact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ds.CoordinateSystem); } WpfMap.MapLayers.Add(lay); if (WpfMap.MapLayers.Count == 1) { Envelope env = lay.Envelope; WpfMap.ZoomToEnvelope(env); } } e.Handled = true; }
private GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation CreateTransformation() { var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); return ctf.CreateFromCoordinateSystems( ProjNet.CoordinateSystems.GeocentricCoordinateSystem.WGS84, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84); }
public static Extent ConvertToMercatore(Extent extent) { NetTopologySuite.Geometries.PrecisionModel precisionModel = new NetTopologySuite.Geometries.PrecisionModel(GeoAPI.Geometries.PrecisionModels.Floating); CoordinateSystem wgs84 = GeographicCoordinateSystem.WGS84 as CoordinateSystem; CoordinateSystem mercatore = ProjectedCoordinateSystem.WebMercator as CoordinateSystem; ICoordinateSystemFactory cFac = new CoordinateSystemFactory(); int SRID_wgs84 = Convert.ToInt32(wgs84.AuthorityCode); //WGS84 SRID int SRID_mercatore = Convert.ToInt32(mercatore.AuthorityCode); //Mercatore SRID ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation transformation = ctFact.CreateFromCoordinateSystems(wgs84, mercatore); NetTopologySuite.Geometries.GeometryFactory factory_wgs84 = new NetTopologySuite.Geometries.GeometryFactory(precisionModel, SRID_wgs84); var bottomLeft = factory_wgs84.CreatePoint(new GeoAPI.Geometries.Coordinate(extent.Left, extent.Bottom, 0)); var topRight = factory_wgs84.CreatePoint(new GeoAPI.Geometries.Coordinate(extent.Right, extent.Top, 0)); double[] coords_bl = transformation.MathTransform.Transform(new double[] { bottomLeft.X, bottomLeft.Y }); double[] coords_tr = transformation.MathTransform.Transform(new double[] { topRight.X, topRight.Y }); NetTopologySuite.Geometries.GeometryFactory factory_mercatore = new NetTopologySuite.Geometries.GeometryFactory(precisionModel, SRID_mercatore); var p1_bl = factory_mercatore.CreatePoint(new GeoAPI.Geometries.Coordinate(coords_bl[0], coords_bl[1])); var p2_tr = factory_mercatore.CreatePoint(new GeoAPI.Geometries.Coordinate(coords_tr[0], coords_tr[1])); return new Extent(p1_bl.X, p1_bl.Y, p2_tr.X, p2_tr.Y); }
public static GisPoint ConvertToMercatore(GisPoint point) { NetTopologySuite.Geometries.PrecisionModel precisionModel = new NetTopologySuite.Geometries.PrecisionModel(GeoAPI.Geometries.PrecisionModels.Floating); CoordinateSystem wgs84 = GeographicCoordinateSystem.WGS84 as CoordinateSystem; CoordinateSystem mercatore = ProjectedCoordinateSystem.WebMercator as CoordinateSystem; ICoordinateSystemFactory cFac = new CoordinateSystemFactory(); int SRID_wgs84 = Convert.ToInt32(wgs84.AuthorityCode); //WGS84 SRID int SRID_mercatore = Convert.ToInt32(mercatore.AuthorityCode); //Mercatore SRID ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation transformation = ctFact.CreateFromCoordinateSystems(wgs84, mercatore); NetTopologySuite.Geometries.GeometryFactory factory_wgs84 = new NetTopologySuite.Geometries.GeometryFactory(precisionModel, SRID_wgs84); var convertedPoint = factory_wgs84.CreatePoint(new GeoAPI.Geometries.Coordinate(point.X, point.Y, 0)); double[] coords = transformation.MathTransform.Transform(new double[] { convertedPoint.X, convertedPoint.Y }); NetTopologySuite.Geometries.GeometryFactory factory_mercatore = new NetTopologySuite.Geometries.GeometryFactory(precisionModel, SRID_mercatore); var p1 = factory_mercatore.CreatePoint(new GeoAPI.Geometries.Coordinate(coords[0], coords[1])); return new GisPoint(p1.X, p1.Y); }
public void TestConversionProjNet() { var csf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var cs1 = csf.CreateFromWkt(Osgb36); var cs2 = csf.CreateFromWkt(WGS84); var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var ct = ctf.CreateFromCoordinateSystems(cs1, cs2); System.Diagnostics.Debug.Assert(ct != null); }
public void TestRealData() { var fdt = GetRealFeatureDataTable(); FillRealDataTable(fdt); var p = new SharpMap.Data.Providers.FeatureProvider(fdt); var m = new SharpMap.Map(new Size(640, 640)); m.Layers.Add(new TileLayer( new OsmTileSource(new OsmRequest(KnownTileServers.Mapnik) /*, new FileCache(@"d:\temp\OSM", "png")*/), "OSM")); var l = new HeatLayer(p, "Data", 0.001f); l.LayerName = "HEAT"; m.Layers.Add(l); #if DotSpatialProjections l.CoordinateTransformation = new DotSpatial.Projections.CoordinateTransformation { Source = ProjectionInfo.FromEpsgCode(4326), Target = ProjectionInfo.FromEpsgCode(3857) }; #else var ctfac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); l.CoordinateTransformation = ctfac.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); #endif l.ZoomMin = 0;// 0.01 * m.GetExtents().Width; l.ZoomMax = /*0.9 * */m.GetExtents().Width; l.OpacityMax = 1; l.OpacityMin = 0.3f; m.ZoomToBox(l.Envelope); for (var i = 0; i < 5; i++) { using (var img = m.GetMap()) { img.Save("RealDataHeat" + i + ".png"); } m.Zoom /= 2; } m.ZoomToExtents(); using (var img = m.GetMap()) { img.Save("RealDataHeat.png"); } }
public static SharpMap.Map InitializeMap(float angle) { var fdt = GetRealFeatureDataTable(); FillRealDataTable(fdt); var p = new SharpMap.Data.Providers.GeometryFeatureProvider(fdt); var m = new SharpMap.Map(); m.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer( new BruTile.Web.OsmTileSource( new BruTile.Web.OsmRequest(KnownTileSource.OpenStreetMap), new BruTile.Cache.FileCache(@"d:\temp\OSM", "png")), "OSM")); m.BackgroundLayer[0].LayerName = "TileLayer with HeatLayer"; //var l = new SharpMap.Layers.HeatLayer(p, r => 0.02f) { LayerName = "HEAT" }; var l = new SharpMap.Layers.HeatLayer(p, "Data", 0.00025f) { LayerName = "HEAT" }; m.Layers.Add(l); #if DotSpatialProjections l.CoordinateTransformation = new DotSpatial.Projections.CoordinateTransformation { Source = DotSpatial.Projections.ProjectionInfo.FromEpsgCode(4326), Target = DotSpatial.Projections.ProjectionInfo.FromEpsgCode(3857) }; #else var ctfac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); l.CoordinateTransformation = ctfac.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); #endif l.ZoomMin = 0;// 0.01 * m.GetExtents().Width; l.ZoomMax = /*0.9 * */m.GetExtents().Width; l.OpacityMax = 0.5f; l.OpacityMin = 0.9f; l.HeatColorBlend = SharpMap.Layers.HeatLayer.Classic; var env = l.Envelope; GeoAPI.Geometries.GeoAPIEx.Grow(env, env.Width * 0.05, env.Height * 0.05); //m.EnforceMaximumExtents = true; //m.MaximumExtents = env; m.ZoomToBox(env); return m; }
public static List<IPoint> projectGPSto23030(List<IPoint> wayPoints) { try { List<IPoint>projectedPoints=new List<IPoint>(); // p_WGS84=new Point(Lon,Lat); string ED50= "PROJCS[\"ED50 / UTM zone 30N\",GEOGCS[\"ED50\",DATUM[\"European_Datum_1950\",SPHEROID[\"International 1924\",6378388,297,AUTHORITY[\"EPSG\",\"7022\"]],AUTHORITY[\"EPSG\",\"6230\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4230\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-3],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"23030\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]"; // string WGS84 = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"; // string ETRS89="PROJCS[\"ETRS89 / UTM zone 30N\",GEOGCS[\"ETRS89\",DATUM[\"European_Terrestrial_Reference_System_1989\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],AUTHORITY[\"EPSG\",\"6258\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4258\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-3],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"25830\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]"; var csf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var cs1 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;//csf.CreateFromWkt(WGS84); var cs2 = csf.CreateFromWkt(ED50); var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var ct = ctf.CreateFromCoordinateSystems(cs1, cs2).MathTransform; foreach(IPoint p in wayPoints) { double[] point=new double[2]; point[0]=p.X; point[1]=p.Y; double[] pointReturn = ct.Transform(point); IPoint projPoint= new Point(pointReturn[0],pointReturn[1]); projPoint.UserData=p.UserData; projectedPoints.Add(projPoint); } return projectedPoints; } catch (Exception ex) { throw ex; } }
private void SetCoordTransAndZoomExtents(Map m, int targetSrid) { m.SRID = targetSrid; var css = Session.Instance.CoordinateSystemServices; var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); for (int i = 0; i < m.Layers.Count(); i++) { var vl = (VectorLayer)m.Layers[i]; if (vl.SRID == targetSrid) { vl.CoordinateTransformation = null; } else { vl.CoordinateTransformation = ctf.CreateFromCoordinateSystems( css.GetCoordinateSystem(vl.SRID), css.GetCoordinateSystem(targetSrid)); } } m.ZoomToExtents(); }
public static List <IPoint> projectGPSto23030(List <IPoint> wayPoints) { try { List <IPoint> projectedPoints = new List <IPoint>(); // p_WGS84=new Point(Lon,Lat); string ED50 = "PROJCS[\"ED50 / UTM zone 30N\",GEOGCS[\"ED50\",DATUM[\"European_Datum_1950\",SPHEROID[\"International 1924\",6378388,297,AUTHORITY[\"EPSG\",\"7022\"]],AUTHORITY[\"EPSG\",\"6230\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4230\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-3],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"23030\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]"; // string WGS84 = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"; // string ETRS89="PROJCS[\"ETRS89 / UTM zone 30N\",GEOGCS[\"ETRS89\",DATUM[\"European_Terrestrial_Reference_System_1989\",SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],AUTHORITY[\"EPSG\",\"6258\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4258\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-3],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"25830\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]"; var csf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var cs1 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84; //csf.CreateFromWkt(WGS84); var cs2 = csf.CreateFromWkt(ED50); var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var ct = ctf.CreateFromCoordinateSystems(cs1, cs2).MathTransform; foreach (IPoint p in wayPoints) { double[] point = new double[2]; point[0] = p.X; point[1] = p.Y; double[] pointReturn = ct.Transform(point); IPoint projPoint = new Point(pointReturn[0], pointReturn[1]); projPoint.UserData = p.UserData; projectedPoints.Add(projPoint); } return(projectedPoints); } catch (Exception ex) { throw ex; } }
private void Convert_To_USNG() { // this function is going to convert our lat/long into both UTM and USNG // we do both here because we need to break up the UTM to convert it into USNG. if (latitude < -80 || latitude > 84) { return; } int zone = Get_Zone(); string band = Get_Band(); var utmFactory = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var wgs84CS = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84; var utmCS = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WGS84_UTM(zone, latitude > 0); var utmTrans = utmFactory.CreateFromCoordinateSystems(wgs84CS, utmCS); double[] utmPoints = utmTrans.MathTransform.Transform(new double[] { (double)longitude, (double)latitude }); double easting = utmPoints[0]; double northing = utmPoints[1]; var ZoneNumber = Get_Zonenumber(); UTM = String.Format("{0}{1} {2:0} {3:0}", zone, band, easting, northing); if (latitude < 0) { northing += NORTHING_OFFSET; } string USNGLetters = Find_Grid_Letters(ZoneNumber, northing, easting); string USNGNorthing = Math.Round(northing).ToString(); USNGNorthing = USNGNorthing.Substring(USNGNorthing.Length - 5).Substring(0, 4); string USNGEasting = Math.Round(easting).ToString(); USNGEasting = USNGEasting.Substring(USNGEasting.Length - 5).Substring(0, 4); USNG = ZoneNumber.ToString() + Get_Band() + USNGLetters + " " + USNGEasting + " " + USNGNorthing; }
private static GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation GetCoordinateTransformation() { //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); var epsg3857 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]"); return ctf.CreateFromCoordinateSystems(epsg4326, epsg3857);
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); } }
public static SqlGeography GeographyToGeography(SqlGeography geog, SqlInt32 toSRID) { using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); // Retrieve the parameters of the source spatial reference system SqlCommand cmd = new SqlCommand("SELECT well_known_text FROM prospatial_reference_systems WHERE spatial_reference_id = @srid", conn); cmd.Parameters.Add(new SqlParameter("srid", geog.STSrid)); String fromWKT = (String)cmd.ExecuteScalar(); // Create the source coordinate system from WKT ICoordinateSystem fromCS = ProjNet.Converters.WellKnownText.CoordinateSystemWktReader.Parse(fromWKT) as ICoordinateSystem; // Retrieve the parameters of the destination spatial reference system cmd.Parameters["srid"].Value = toSRID; String toWKT = (String)cmd.ExecuteScalar(); cmd.Dispose(); // Create the destination coordinate system from WKT ICoordinateSystem toCS = ProjNet.Converters.WellKnownText.CoordinateSystemWktReader.Parse(toWKT) as ICoordinateSystem; // Create a CoordinateTransformationFactory: ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctfac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); // Create the transformation instance: ProjNet.CoordinateSystems.Transformations.ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(fromCS, toCS); // create a sink that will create a geometry instance SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid((int)toSRID); // create a sink to do the shift and plug it in to the builder TransformGeographyToGeographySink s = new TransformGeographyToGeographySink(trans, b); // plug our sink into the geometry instance and run the pipeline geog.Populate(s); // the end of our pipeline is now populated with the shifted geometry instance return(b.ConstructedGeography); } }
public static void InitializeBackgroundMap(ref SharpMap.Forms.MapBox mapBox1) { string GSHHGFileName = @"d:\Users\dwoodall\Documents\GSHHG\gshhg-shp-2.3.5-1\GSHHS_shp\f\GSHHS_f_L1.shp"; // this is the default area to be displayed. do not make it bigger unless you want to wait forever for it to render GeoAPI.Geometries.Envelope envelope = new GeoAPI.Geometries.Envelope(-75.0d, -80.0d, 23.0d, 28.0d); //ShapeFile GSHHGData = new ShapeFile(GSHHGFileName); SharpMap.Layers.VectorLayer GSHHGLayer = new SharpMap.Layers.VectorLayer("GSHHG"); GSHHGLayer.DataSource = new SharpMap.Data.Providers.ShapeFile(GSHHGFileName, true); if (!GSHHGLayer.DataSource.IsOpen) { GSHHGLayer.DataSource.Open(); } //Create the style for Land SharpMap.Styles.VectorStyle landStyle = new SharpMap.Styles.VectorStyle(); landStyle.Fill = new System.Drawing.SolidBrush(S52.colorMaptoRGBI(S52.ColorName.LANDF)); landStyle.Outline = new System.Drawing.Pen(System.Drawing.Color.Black); //Create the style for Water SharpMap.Styles.VectorStyle waterStyle = new SharpMap.Styles.VectorStyle(); waterStyle.Fill = new System.Drawing.SolidBrush(S52.colorMaptoRGBI(S52.ColorName.DEPMD)); //Create the default style SharpMap.Styles.VectorStyle defaultStyle = new SharpMap.Styles.VectorStyle(); defaultStyle.Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Red); //Create the theme items Dictionary <string, SharpMap.Styles.IStyle> styles = new Dictionary <string, SharpMap.Styles.IStyle>(); styles.Add("land", landStyle); styles.Add("water", waterStyle); styles.Add("default", defaultStyle); GSHHGLayer.Style = landStyle; GSHHGLayer.Style.EnableOutline = true; mapBox1.BackColor = S52.colorMaptoRGB(S52.ColorName.UINFB); mapBox1.Map.Layers.Add(GSHHGLayer); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); GSHHGLayer.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); GSHHGLayer.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84); //mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer( // new BruTile.Web.OsmTileSource(), "OSM")); //mapBox1.Map.ZoomToBox(envelope); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan; }
private static SharpMap.Map InitializeMapOsmWithXls(float angle) { var map = new SharpMap.Map(); var tileLayer = new SharpMap.Layers.TileAsyncLayer(new BruTile.Web.OsmTileSource(), "TileLayer - OSM with XLS"); map.BackgroundLayer.Add(tileLayer); //Get data from excel var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Cities.xls"); var ds = new System.Data.DataSet("XLS"); using (var cn = new System.Data.OleDb.OleDbConnection(xlsPath)) { cn.Open(); using (var da = new System.Data.OleDb.OleDbDataAdapter(new System.Data.OleDb.OleDbCommand("SELECT * FROM [Cities$]", cn))) da.Fill(ds); } #if !DotSpatialProjections //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); var epsg3857 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]"); var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3857); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) { continue; } var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"]) }; coords = ct.MathTransform.Transform(coords); row["X"] = coords[0]; row["Y"] = coords[1]; } #else var epsg4326 = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984; var epsg3857 = DotSpatial.Projections.ProjectionInfo.FromEsriString("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]"); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) { continue; } var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"]) }; DotSpatial.Projections.Reproject.ReprojectPoints(coords, null, epsg4326, epsg3857, 0, 1); row["X"] = coords[0]; row["Y"] = coords[1]; } #endif //Add Rotation Column ds.Tables[0].Columns.Add("Rotation", typeof(float)); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { row["Rotation"] = -angle; } //Set up provider var xlsProvider = new SharpMap.Data.Providers.DataTablePoint(ds.Tables[0], "OID", "X", "Y"); var xlsLayer = new SharpMap.Layers.VectorLayer("XLS", xlsProvider) { Style = { Symbol = SharpMap.Styles.VectorStyle.DefaultSymbol } }; //Add layer to map map.Layers.Add(xlsLayer); var xlsLabelLayer = new SharpMap.Layers.LabelLayer("XLSLabel") { DataSource = xlsProvider, LabelColumn = "Name", PriorityColumn = "Population", Style = { CollisionBuffer = new System.Drawing.SizeF(2f, 2f), CollisionDetection = true }, LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection }; map.Layers.Add(xlsLabelLayer); map.ZoomToBox(tileLayer.Envelope); return(map); }
private static SharpMap.Map InitializeMapOsmWithXls(float angle) { var map = new SharpMap.Map(); var tileLayer = new SharpMap.Layers.TileAsyncLayer(new BruTile.Web.OsmTileSource(), "TileLayer - OSM with XLS"); map.BackgroundLayer.Add(tileLayer); //Get data from excel var xlsPath = string.Format(XlsConnectionString, System.IO.Directory.GetCurrentDirectory(), "GeoData\\Cities.xls"); var ds = new System.Data.DataSet("XLS"); using (var cn = new System.Data.OleDb.OleDbConnection(xlsPath)) { cn.Open(); using (var da = new System.Data.OleDb.OleDbDataAdapter(new System.Data.OleDb.OleDbCommand("SELECT * FROM [Cities$]", cn))) da.Fill(ds); } #if !DotSpatialProjections //The SRS for this datasource is EPSG:4326, therefore we need to transfrom it to OSM projection var ctf = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); var cf = new ProjNet.CoordinateSystems.CoordinateSystemFactory(); var epsg4326 = cf.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); var epsg3857 = cf.CreateFromWkt("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]"); var ct = ctf.CreateFromCoordinateSystems(epsg4326, epsg3857); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) continue; var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"])}; coords = ct.MathTransform.Transform(coords); row["X"] = coords[0]; row["Y"] = coords[1]; } #else var epsg4326 = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984; var epsg3857 = DotSpatial.Projections.ProjectionInfo.FromEsriString("PROJCS[\"Popular Visualisation CRS / Mercator\", GEOGCS[\"Popular Visualisation CRS\", DATUM[\"Popular Visualisation Datum\", SPHEROID[\"Popular Visualisation Sphere\", 6378137, 0, AUTHORITY[\"EPSG\",\"7059\"]], TOWGS84[0, 0, 0, 0, 0, 0, 0], AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], UNIT[\"degree\", 0.0174532925199433, AUTHORITY[\"EPSG\", \"9102\"]], AXIS[\"E\", EAST], AXIS[\"N\", NORTH], AUTHORITY[\"EPSG\",\"4055\"]], PROJECTION[\"Mercator\"], PARAMETER[\"False_Easting\", 0], PARAMETER[\"False_Northing\", 0], PARAMETER[\"Central_Meridian\", 0], PARAMETER[\"Latitude_of_origin\", 0], UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], AXIS[\"East\", EAST], AXIS[\"North\", NORTH], AUTHORITY[\"EPSG\",\"3857\"]]"); foreach (System.Data.DataRow row in ds.Tables[0].Rows) { if (row["X"] == DBNull.Value || row["Y"] == DBNull.Value) continue; var coords = new[] { Convert.ToDouble(row["X"]), Convert.ToDouble(row["Y"])}; DotSpatial.Projections.Reproject.ReprojectPoints(coords, null, epsg4326, epsg3857, 0, 1); row["X"] = coords[0]; row["Y"] = coords[1]; } #endif //Add Rotation Column ds.Tables[0].Columns.Add("Rotation", typeof (float)); foreach (System.Data.DataRow row in ds.Tables[0].Rows) row["Rotation"] = -angle; //Set up provider var xlsProvider = new SharpMap.Data.Providers.DataTablePoint(ds.Tables[0], "OID", "X", "Y"); var xlsLayer = new SharpMap.Layers.VectorLayer("XLS", xlsProvider) {Style = {Symbol = SharpMap.Styles.VectorStyle.DefaultSymbol}}; //Add layer to map map.Layers.Add(xlsLayer); var xlsLabelLayer = new SharpMap.Layers.LabelLayer("XLSLabel") { DataSource = xlsProvider, LabelColumn = "Name", PriorityColumn = "Population", Style = { CollisionBuffer = new System.Drawing.SizeF(2f, 2f), CollisionDetection = true }, LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection }; map.Layers.Add(xlsLabelLayer); map.ZoomToBox(tileLayer.Envelope); return map; }
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."); }
public void TestTransformation() { var m = new SharpMap.Map(new System.Drawing.Size(640, 320)); var l = new SharpMap.Layers.Symbolizer.PuntalVectorLayer("l", new SharpMap.Data.Providers.GeometryProvider(m.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(0, 51.478885))), SharpMap.Rendering.Symbolizer.PathPointSymbolizer.CreateCircle(System.Drawing.Pens.Aquamarine, System.Drawing.Brushes.BurlyWood, 24)); var ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(); l.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator); l.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84); m.Layers.Add(new SharpMap.Layers.TileLayer(new BruTile.Web.OsmTileSource(),"b")); m.Layers.Add(l); var e = new GeoAPI.Geometries.Envelope(-0.02, 0.02, 51.478885 - 0.01, 51.478885 + 0.01); e = GeoAPI.CoordinateSystems.Transformations.GeometryTransform.TransformBox(e, l.CoordinateTransformation.MathTransform); m.ZoomToBox(e); m.GetMap().Save("Greenwich.png", System.Drawing.Imaging.ImageFormat.Png); }