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); } }
protected void ddlProjection_SelectedIndexChanged(object sender, EventArgs e) { //Transform current view to new coordinate system and zoom to the transformed box string PreviousProj = ViewState["currentProj"].ToString(); string SelectedProj = ddlProjection.SelectedValue; //Points defining the current view Point left = new Point(myMap.Envelope.MinX, myMap.Center.Y); Point right = new Point(myMap.Envelope.MaxX, myMap.Center.Y); Point center = myMap.Center; if (PreviousProj != "Pseudo") { //Transform current view back to geographic coordinates ProjNet.CoordinateSystems.Transformations.ICoordinateTransformation trans = GetTransform(PreviousProj); left = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MinX, myMap.Center.Y), trans.MathTransform.Inverse()); right = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MaxX, myMap.Center.Y), trans.MathTransform.Inverse()); center = GeometryTransform.TransformCoordinate(myMap.Center, trans.MathTransform.Inverse()); } //If both PreviousSRID and SelectedSRID are projected coordsys, first transform to geographic if (SelectedProj == "Pseudo") { myMap.Center = center; myMap.Zoom = Math.Abs(right.X - left.X); } else //Project coordinates to new projection { //Transform back to geographic and over to new projection ProjNet.CoordinateSystems.Transformations.ICoordinateTransformation trans = GetTransform(SelectedProj); left = GeometryTransform.TransformCoordinate(left, trans.MathTransform); right = GeometryTransform.TransformCoordinate(right, trans.MathTransform); center = GeometryTransform.TransformCoordinate(center, trans.MathTransform); myMap.Center = center; myMap.Zoom = Math.Abs(right.X - left.X); var envelopeGcs = GeometryTransform.TransformBox(myMap.Envelope, trans.MathTransform.Inverse()); litEnvelopeLatLong.Text = envelopeGcs.ToString(); } GenerateMap(); }