[gdal-dev] Ogr: ExecuteSQL with MSSQL and geometry column

Benjamin benjamin.lux at maxsea.fr
Wed Jul 18 06:01:04 PDT 2012

Sorry, I was not clear. 

I resume from the beginning.

I work with a warper C# of Ogr.
I have a table dbo.MY_TABLE with 3 fields (D, CHART_ID, WKB_GEOMETRY) in a
SQL server Database (in SQL Azure exactly).

*My aim is to work with a copy* (in a 
http://www.gdal.org/ogr/drv_memory.html Memory DataSource ) of a SQL
statement result executed on a MSSQL DataSource.

Actually I can work with usuals methods with this MSSQL DataSource.
Note that I have not geometry_column table in my SQL Serveur DataBase, I use 
http://www.gdal.org/ogr/drv_mssqlspatial.html driver options .

string driverOptions = 
serverDataSource = Ogr.Open("MSSQL:" + connectionString + driverOptions),

serveurDataSource.GetLayerCount(); // It works fine.

So I tried a call to 
SQLExecute() .
string request = "*SELECT * FROM MY_TABLE WHERE CHART_ID = 'ID'*"
Layer serverLayer = serverDataSource.ExecuteSQL(request, null, "");

But unfortunately, I have not geometry field in the result.
serveurLayer.GetGeometryColumn();  //*return ""*
serveurLayer.GetGeomType();          //*return wkbUnknown*

But I have my 3 fields in serveurLayer !
serverLayer.GetLayerDefn().GetFieldCount();                //return 3
serverLayer.GetLayerDefn().GetFieldDefn(0).GetName(); //return "D"
serverLayer.GetLayerDefn().GetFieldDefn(1).GetName(); //return "CHART_ID"
serverLayer.GetLayerDefn().GetFieldDefn(2).GetName(); //*return

So I want to copy this layer in a Memory Layer to minimise acces to my SQL
Server and work faster.
Driver memDriver = Ogr.GetDriverByName("Memory");
DataSource memDataSource =
memDriver.CreateDataSource(GetWorkPath("nomimprobale"), null);
Layer memLayer = memDataSource.CopyLayer(serverLayer, layerName, null);

Now,* I have to fetch my geometries*, so have thought to a trick like this :
byte[] memWkb =
Geometry memGeom = Geometry.CreateFromWkb(memWkb); 
Because I can't use memFeature.GetGeometryRef() 
memFeature.GetLayerCount();                                // return 39
Feature memFeature = memLayer.GetNextFeature();  // don't return null
memFeature.GetGeometryRef();                             // *return null*

But I don't know if I have to use UTF8, ASCII, Unicode or other.
If you know what encoding is use, or better an other trick to have a
geometry in my memLayer or serverLayer I heard you with interest !

Sorry for my spelling, English isn't my native tongue language.


Ps : link to the thread: 

Even Rouault wrote
> Selon Benjamin <benjamin.lux@>:
>> "You can access the geometry like with any other ogr feature."
> Hopefully, memFeature.GetGeometryRef() should do it !
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at .osgeo
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

View this message in context: http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831p4989072.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.

More information about the gdal-dev mailing list