Wednesday 30 January 2019

geometry - C# library for converting geom field of SQLServer into set of google latlng


Is there any C# library which can convert the geometry field into set of google latlngs? I have stored a shapefile into SQLserver table. From Sqlserver i want to read the geometry field data and want to convert this geom data into set of google latlngs in my C# application. Finally i want to save these google latlngs into another table in SQLServer.



Answer



What about SQL Server Spatial Tools? You can incorporate it into SQL calls or use it in .NET code as well. The SqlProjection class might work for this. From the sample projection_example.sql in the source .zip:


-- Project point and linestring using Albers Equal Area projection

declare @albers Projection
set @albers = Projection::AlbersEqualArea(0, 0, 0, 60)

select @albers.Project('POINT (45 30)').ToString()
select @albers.Unproject(@albers.Project('LINESTRING (10 0, 10 10)')).ToString()
select @albers.ToString()

Also, look at this post on the SQL Server Spatial Tools discussions site, here is some code from that post:


--CREATE A GEOGRAPHIC POINT IN WGS84 LONGITUDE, LATITUDE COORDINATES
DECLARE @point GEOGRAPHY;

SET @point = GEOGRAPHY::STGeomFromText('POINT(5 52)',4326);

--PROJECT THE GEOGRAPHIC POINT TO A UNIT SPHERE MERCATOR PROJECTION
DECLARE @point_mercator GEOMETRY;
DECLARE @mercator Projection;
SET @mercator = Projection::Mercator(0) -- Initialize projection (0 = Longitude of Central Meridian)
SET @point_mercator = @mercator.Project(@point) -- Perform the projection
SELECT @point_mercator.STAsText() UNIT_SPHERE_MERCATOR
--UNIT_SPHERE_MERCATOR
--POINT (0.087266462599716474 1.0661617106056684)


--SCALE THE PROJECTED COORDINATES FROM THE UNIT SPHERE TO THE WGS84 SPHERE
DECLARE @point_mercator_wgs84 GEOMETRY;
DECLARE @a FLOAT;
SET @a = 6378137 -- Spherical Mercator Radius in meters
SET @point_mercator_wgs84 = AffineTransform::Scale(@a, @a).Apply(@point_mercator) -- perform the scaling operation
SELECT @point_mercator_wgs84.STAsText() AS WGS84_MERCATOR
--WGS84_MERCATOR
--POINT (556597.45396636787 6800125.4543973068)


To get the new transformed points to another table, you can do that with a SQL call. Actually, you could probably do all of what you need to do in one stored procedure using the SqlProjection class.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...