how to convert sqlgeometry to esri geometry

6956
7
01-31-2011 05:35 AM
SolidSmoke
New Contributor
do we have any method to convert sqlgeometry to esri geometry? anyone pls help me
0 Kudos
7 Replies
DaveRabrun
New Contributor
What I had to do in a previous project was :

1) Query the desired geometries in SQL, using  a function that returns the geometry as  Well-Known Text (WKT).

In MSSQL the function is STAsText function. "Select STAsText(geometryColumn) as geometry from geometrydatabase;"

2) Use a WKT parser to create an object from the query results. Then convert the WKT parsed object to an ESRI.Arcgis.Client.Geometry.

I used a WKT parser that is part of the SharpMap Framework.
ObservableCollection<ESRI.ArcGIS.Client.Geometry.Geometry> geometryList= new ObservableCollection<ESRI.ArcGIS.Client.Geometry.Geometry>();


foreach(string polygon in sqlStringPolygons) 
{
     
     SharpMap.Geometries.Geometry wktGeom = SharpMap.Converters.WellKnownText.GeometryFromWKT.Parse(polygon); 

     Esri.ArcGIS.Client.Geometry.Geometry geom = sharp2esriGeometry(wktGeom);

     geomtryList.Add(geom);
}


sharp2esriGeometry would be where the actual conversion takes place, points in the WKT converter would be converted to ESRI MapPoints. Also make sure everything is in the right projection for display purposes.

Alot of overhead, and I'm sure there are other (better?) ways to do this, but it has worked for me in the past.
0 Kudos
dotMorten_esri
Esri Notable Contributor
I would recommend against using WKT. Parsing it is a huge overhead. It's MUCH faster to parse WKB, not to mention building a parser for it is much simpler. First converting from SqlGeometry, to String, back to numbers, populate a SharpMap geometry, then re-create the geometry as ESRI geometry is quite an overhead.
The simplest approach is to use the SqlGeometry classes directly in your project, and parse those directly into ESRI geometry. That way you only will have one conversion., don't have to mess with Well-known-* or other geometry libraries.

Worst case, SharpMap is open source, so you could just take the SharpMap.Converters.WellKnownText.GeometryFromWKT and tweak it to return ESRI geometry instead (or even better use it's WKB classes). That shouldn't be much work.
(Still I recommend using SqlGeometry directly, instead of converting to WKT/B).
0 Kudos
SolidSmoke
New Contributor
actually i'm new to these stuff and need your help to parse SqlGeometry directly into ESRI geometry. if you can give me a sample code, that wud be very helpfull. thanx in advance.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Sorry I don't have anything at hand here. I just know it can be done. The best you can do is first get your head around how SqlGeometry works and how it represents points, lines and polygons (MSDN has lots of doc on this). Then do the same for ESRI Geometry. When you got your head around those two things, it should be fairly straightforward for you to do.
0 Kudos
by Anonymous User
Not applicable
How can I use SQL geometries directly in a silverlight app? Didn't think Microsoft.SqlServer.Types was supported in SL.
0 Kudos
TerryGiles
Occasional Contributor III
Would it work for either of you to create a QueryLayer in a MXD and publish that as a Map Service?

Here's the doc for a Query Layer in v10 - http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s50000000n000000.htm
0 Kudos
NickG
by
New Contributor II
This might not have been available in 2011, but in case anyone else runs across this thread...

SqlGeometry sqlGeo = (SqlGeometry)myReader["SHAPE"];
IGeometryFactory factory = new GeometryEnvironmentClass();
IGeometry geom = new PolygonClass();
int countout;
factory.CreateGeometryFromWkbVariant(sqlGeo.STAsBinary().Value, out geom, out countout);
0 Kudos