Benjamin,<br><br>First thing: You don't need to use a memory datasource here. OGR doesn't make round trips to the database server every time you run GetNextFeature(). Do that first. I'm sure it will make it less complicated.<br>
<br>I don't understand why you are getting the null. Hopefully we can figure that out after you simplify your code by removing the memory driver stuff.<br><br><div class="gmail_quote">On Wed, Jul 18, 2012 at 8:52 PM, Benjamin LUX <span dir="ltr"><<a href="mailto:Benjamin.lux@maxsea.fr" target="_blank">Benjamin.lux@maxsea.fr</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div link="blue" vlink="purple" lang="FR"><div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Hi again </span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"" lang="EN-US">Chaitanya,</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">If you look my previous mail you can see the code.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">But in a compact way this is a copy/paste of my running code.<u></u><u></u></span></p><p class="MsoNormal">
<span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Note that RoleEnvironment.GetConfigurationSettingValue(“****”) just provide me parametrables strings (like argv[n]).<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US">[code]<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Ogr</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">.RegisterAll();<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue" lang="EN-US">string</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> option = <span style="color:#2b91af">String</span>.Format(<span style="color:#a31515">"Tables=dbo.{0}(WKB_GEOMETRY);GeometryFormat=wkb;"</span>,<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> <span style="color:#2b91af">RoleEnvironment</span>.GetConfigurationSettingValue(<span style="color:#a31515">"CatalogueRclAllName"</span>)<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> );<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">RclDataSource = <span style="color:#2b91af">Ogr</span>.Open(<span style="color:#2b91af">String</span>.Format(<span style="color:#a31515">"MSSQL:{0}{1}"</span>, <span style="color:#2b91af">RoleEnvironment</span>.GetConfigurationSettingValue(<span style="color:#a31515">"DataBaseConnectionString"</span>), option), 0);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue" lang="EN-US">string</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> request = <span style="color:#2b91af">String</span>.Format(<span style="color:#a31515">"SELECT * FROM {0} WHERE CHART_ID = '{1}'"</span>,<u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:35.4pt;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">RoleEnvironment</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">.GetConfigurationSettingValue(<span style="color:#a31515">"CatalogueRclAllName"</span>),<u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:35.4pt;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#a31515" lang="EN-US">"Z15F43AJ"</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Layer</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> serverLayer = RclDataSource.ExecuteSQL(request, <span style="color:blue">null</span>, <span style="color:#a31515">""</span>);<u></u><u></u></span></p>
<div class="im"><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Driver</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> memDriver = <span style="color:#2b91af">Ogr</span>.GetDriverByName(<span style="color:#a31515">"Memory"</span>);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">DataSource</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> memDataSource = memDriver.CreateDataSource(GetWorkPath(<span style="color:#a31515">"nomimprobale"</span>), <span style="color:blue">null</span>);<u></u><u></u></span></p>
</div><p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Layer</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> memLayer = memDataSource.CopyLayer(serverLayer, serverLayer.GetName(), <span style="color:blue">null</span>);<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Feature</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> memFeature = memLayer.GetNextFeature();<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue" lang="EN-US">string</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">[] namesColumnsArray = <span style="color:blue">new</span> <span style="color:blue">string</span>[5];<u></u><u></u></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas;color:blue" lang="EN-US">for</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> (<span style="color:blue">int</span> i = 0; i < memFeature.GetFieldCount(); i++)<u></u><u></u></span></p>
<p class="MsoNormal" style="text-indent:35.4pt;text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US">namesColumnsArray[i] = memFeature.GetFieldDefnRef(i).GetName();<u></u><u></u></span></p><p class="MsoNormal" style="text-autospace:none">
<span style="font-size:9.5pt;font-family:Consolas;color:#2b91af" lang="EN-US">Geometry</span><span style="font-size:9.5pt;font-family:Consolas" lang="EN-US"> memGeometry = memFeature.GetGeometryRef(); <u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US">[/code]<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Results :<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"> namesColumnsArray = { D , CHART_ID, WKB_GEOMETRY }<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"> memGeometry = null<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">I don’t see what I can add to this :/<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">If you want more details said me.<u></u><u></u></span></p>
<div class="im"><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Link to the thread : </span><a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html" target="_blank"><span lang="EN-US">http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html</span></a><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US"><u></u> <u></u></span></p></div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Best regards,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"" lang="EN-US">Benjamin<span style="color:#1f497d"><u></u><u></u></span></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">De :</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Chaitanya kumar CH [mailto:<a href="mailto:chaitanya.ch@gmail.com" target="_blank">chaitanya.ch@gmail.com</a>] <br>
<b>Envoyé :</b> mercredi 18 juillet 2012 16:53<br><b>À :</b> Benjamin LUX<br><b>Cc :</b> gdal dev<br><b>Objet :</b> Re: Ogr: ExecuteSQL with MSSQL and geometry column<u></u><u></u></span></p><div><div class="h5"><p class="MsoNormal">
<u></u> <u></u></p><p class="MsoNormal" style="margin-bottom:12.0pt">Hi Benjamin,<br><br>Can you provide some more details?<br>How did you try to retrieve the geometry?<u></u><u></u></p><div><p class="MsoNormal">On Wed, Jul 18, 2012 at 8:02 PM, <<a href="mailto:benjamin.lux@maxsea.fr" target="_blank">benjamin.lux@maxsea.fr</a>> wrote:<u></u><u></u></p>
<p class="MsoNormal"><quote author='Benjamin'><br>memFeature.GetGeometryRef(); // return null<br></quote><br><br>Sorry, but no.<br>After a call to SQLExecute(), Ogr framwork doesn't recognize any geometry in my feature.<br>
If you have a clue why that ?<br><br><quote author='chaitanya_ch'><u></u><u></u></p><div><div><p class="MsoNormal">Benjamin,<br><br>The mssql driver already does the conversion from wkb in the database to<br>
the native ogr geometry format. You don't have to create a temporary wkb<br>geometry and then the ogr geometry.<br><br>You can simply do this:<br>[code]<br>Geometry memGeom = memFeature.GetGeometryRef()<br>[/code]<br>
<br>On Wed, Jul 18, 2012 at 6:31 PM, Benjamin <<a href="mailto:benjamin.lux@maxsea.fr" target="_blank">benjamin.lux@maxsea.fr</a>> wrote:<br><br>> Sorry, I was not clear.<br>><br>> I resume from the beginning.<br>
><br>> I work with a warper C# of Ogr.<br>> I have a table dbo.MY_TABLE with 3 fields (D, CHART_ID, WKB_GEOMETRY) in a<br>> SQL server Database (in SQL Azure exactly).<br>><br>> *My aim is to work with a copy* (in a<br>
> <a href="http://www.gdal.org/ogr/drv_memory.html" target="_blank">http://www.gdal.org/ogr/drv_memory.html</a> Memory DataSource ) of a SQL<br>> statement result executed on a MSSQL DataSource.<br>><br>> Actually I can work with usuals methods with this MSSQL DataSource.<br>
> Note that I have not geometry_column table in my SQL Serveur DataBase, I<br>> use<br>> <a href="http://www.gdal.org/ogr/drv_mssqlspatial.html" target="_blank">http://www.gdal.org/ogr/drv_mssqlspatial.html</a> driver options .<br>
><br>> [code]<br>> string driverOptions =<br>> "Tables=dbo.MY_TABLE(WKB_GEOMETRY);GeometryFormat=wkb;"<br>> serverDataSource = Ogr.Open("MSSQL:" + connectionString + driverOptions),<br>> 0);<br>
><br>> serveurDataSource.GetLayerCount(); // It works fine.<br>> [/code]<br>><br>> So I tried a call to<br>><br>> <a href="http://www.gdal.org/ogr/classOGRDataSource.html#aa6acc228db6513784a56ce12334a8c33" target="_blank">http://www.gdal.org/ogr/classOGRDataSource.html#aa6acc228db6513784a56ce12334a8c33</a><br>
> SQLExecute() .<br>> [code]<br>> string request = "*SELECT * FROM MY_TABLE WHERE CHART_ID = 'ID'*"<br>> Layer serverLayer = serverDataSource.ExecuteSQL(request, null, "");<br>> [/code]<br>
><br>> But unfortunately, I have not geometry field in the result.<br>> [code]<br>> serveurLayer.GetGeometryColumn(); //*return ""*<br>> serveurLayer.GetGeomType(); //*return wkbUnknown*<br>
> [/code]<br>><br>> But I have my 3 fields in serveurLayer !<br>> [code]<br>> serverLayer.GetLayerDefn().GetFieldCount(); //return 3<br>> serverLayer.GetLayerDefn().GetFieldDefn(0).GetName(); //return "D"<br>
> serverLayer.GetLayerDefn().GetFieldDefn(1).GetName(); //return "CHART_ID"<br>> serverLayer.GetLayerDefn().GetFieldDefn(2).GetName(); //*return<br>> "WKB_GEOMETRY"*<br>> [/code]<br>><br>
> So I want to copy this layer in a Memory Layer to minimise acces to my SQL<br>> Server and work faster.<br>> [code]<br>> Driver memDriver = Ogr.GetDriverByName("Memory");<br>> DataSource memDataSource =<br>
> memDriver.CreateDataSource(GetWorkPath("nomimprobale"), null);<br>> Layer memLayer = memDataSource.CopyLayer(serverLayer, layerName, null);<br>> [/code]<br>><br>><br>> Now,* I have to fetch my geometries*, so have thought to a trick like this<br>
> :<br>> [code]<br>> byte[] memWkb =<br>><br>> System.Text.Encoding.UTF8.GetBytes(memFeature.GetFieldAsString("WKB_GEOMETRY"));<br>> Geometry memGeom = Geometry.CreateFromWkb(memWkb);<br>> [/code]<br>
> Because I can't use memFeature.GetGeometryRef()<br>> [code]<br>> memFeature.GetLayerCount(); // return 39<br>> Feature memFeature = memLayer.GetNextFeature(); // don't return null<br>
> memFeature.GetGeometryRef(); // *return null*<br>> [/code]<br>><br>> But I don't know if I have to use UTF8, ASCII, Unicode or other.<br>> If you know what encoding is use, or better an other trick to have a<br>
> geometry in my memLayer or serverLayer I heard you with interest !<br>><br>><br>> Sorry for my spelling, English isn't my native tongue language.<br>><br>> Benjamin<br>><br>><br>> Ps : link to the thread:<br>
><br>> <a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html</a><br>
><br>> <a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831.html</a><br>
><br>><br>><br>> Even Rouault wrote<br>> ><br>> > Selon Benjamin <benjamin.lux@>:<br>> ><br>> >> "You can access the geometry like with any other ogr feature."<br>
> >><br>> ><br>> > Hopefully, memFeature.GetGeometryRef() should do it !<br>> > _______________________________________________<br>> > gdal-dev mailing list<br>> > <a href="mailto:gdal-dev@.osgeo" target="_blank">gdal-dev@.osgeo</a><br>
> > <a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>> ><br>><br>><br>> --<br>> View this message in context:<br>> <a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831p4989072.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831p4989072.html</a><br>
> Sent from the GDAL - Dev mailing list archive at Nabble.com.<br>> _______________________________________________<br>> gdal-dev mailing list<br>> <a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
> <a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>><br><br><br><br>_______________________________________________<br>gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br><a href="http://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/gdal-dev</a><u></u><u></u></p>
</div></div><p class="MsoNormal"></quote><br>Quoted from:<br><a href="http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831p4989092.html" target="_blank">http://osgeo-org.1560.n6.nabble.com/gdal-dev-Ogr-ExecuteSQL-with-MSSQL-and-geometry-column-tp4988831p4989092.html</a><u></u><u></u></p>
</div><p class="MsoNormal"><br><br clear="all"><br></p></div></div></div></div></blockquote></div><br>-- <br>Best regards,<br>Chaitanya kumar CH.<br><br>+91-9494447584<br>17.2416N 80.1426E<br>