<div dir="ltr"><div dir="ltr">Just realised that I excluded the wider from the group accidentally - so added them back in .<br><br>> I looked a bit more into the mongodb driver. I think I would prefer not using GDAL for saving into the database directly. And only using GDAL for > importing different formats and transforming between projections.<br><br>Makes sense.<br><br>I must admit - since at the end you are deserializing the GeoJSON into a FeatureCollection object I personally would avoid all of this and just write the features directly to the FeatureCollection object.<br><br>But ..</div><div class="gmail_quote"><div dir="ltr" class="gmail_attr"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I found this earlier today and got the same exception as him at the Marshal.Copy line (the Python version uses <span style="color:rgb(0,0,0);white-space:pre-wrap">VSIFReadL)</span>. <br><div><a href="https://lists.osgeo.org/pipermail/gdal-dev/2018-July/048838.html" target="_blank">https://lists.osgeo.org/pipermail/gdal-dev/2018-July/048838.html</a><br></div><div><i><br></i></div><div><pre style="white-space:pre-wrap;color:rgb(0,0,0)"><i> var bufPtr = Gdal.VSIFOpenL(outputRasterFileName, "rb"); //outputRasterFileName is now "/vsimem/file.tiff"
Gdal.VSIFSeekL(bufPtr, 0, 2); // seek to end
var size = Gdal.VSIFTellL(bufPtr);
Gdal.VSIFSeekL(bufPtr, 0, 0); // seek to beginning
Gdal.VSIFCloseL(bufPtr);
var data = new byte[size];
Marshal.Copy(bufPtr, data, 0, size);
Gdal.Unlink(outputRasterFileName);</i> </pre></div></div></blockquote><div><br></div><div>Could that be because you are closing the buffer with `Gdal.VSIFCloseL(bufPtr)`? Why close it before you read it?<br><br>Did you get a valid pointer before closing it? What was the value of size?</div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><pre style="white-space:pre-wrap;color:rgb(0,0,0)"> </pre></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><pre style="white-space:pre-wrap;color:rgb(0,0,0)"><br></pre><pre style="white-space:pre-wrap;color:rgb(0,0,0)"><br></pre></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Den ons 11 nov. 2020 kl 14:44 skrev Paul Harwood <<a href="mailto:runette@gmail.com" target="_blank">runette@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">The MongoDB driver is definitely the way to go if you can :)<br><br>Or - since you seem to have a FeatureCollection class in the last line - just right the Features directly to that?<br><br>But if all of that fails - I am not sure you are right about VSIFOpenL - did you actually try it? It is passing a pointer to the buffer. That suggest that it expects you to read from the pointer yourself - in the c# code. I am not sure why you would pass a pointer from the open method and then require a "read" - that is only going to return a pointer to the same thing again? </div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 11 Nov 2020 at 12:36, Christian Sörensen <<a href="mailto:christian@uxproductions.se" target="_blank">christian@uxproductions.se</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I looked at VSIFOpenL, but the C# version seems to lack VSIFReadL, which is required for actually reading from the unmanaged virtual file memory after opening it.<div><br></div><div>The last tip looked promising, I will have to take a look at the MongoDB driver. If it works it could be a good solution.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Den ons 11 nov. 2020 kl 12:55 skrev Paul Harwood <<a href="mailto:runette@gmail.com" target="_blank">runette@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Customers! What can you say about Customers! :) Seems the worst of all possible worlds to me - but who are we to argue :)</div><div dir="ltr"><br></div><div dir="ltr">However - there does appear to be `IntPtr Gdal.VSIFOpenL(utf8_path, pszMode )` <br><br>I have never used it - so I don't know what it does but the name does suggest that it is what you are looking for. You would probably have to do something like:</div><div dir="ltr"><br></div><div>I have no guarantee that this would work or provide anything you want.<br><br>BTW -I am bit confused that at the end you then deserialize the JSON - which would surely turn it back from a string into objects? Are you sure you need to do this? Are you sure something like <a href="https://gdal.org/drivers/vector/mongodbv3.html#vector-mongodbv3" target="_blank">https://gdal.org/drivers/vector/mongodbv3.html#vector-mongodbv3</a> won't do the job?</div><div dir="ltr"><br></div><div>using System.Runtime.InteropServices;</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div> <i> private FeatureCollection ImportShapeFile(IEnumerable<ImportFile> files)<br> {<br> var tempJsonFilename = $"/vsimem/temp.json";<br><br> GdalConfiguration.ConfigureGdal();<br> GdalConfiguration.ConfigureOgr();<br><br> try<br> {<br></i></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><i> // Load shape<br> Driver shpDriver = Ogr.GetDriverByName("ESRI Shapefile");<br> var shpFile = files.First(f => f.Filename.EndsWith(".shp"));<br> Datasource shpDatasource = Ogr.Open($"/vsimem/{shpFile.Filename}", 0);<br> if (shpDatasource == null)<br> return null;<br><br> Layer shpLayer = shpDatasource.GetLayerByIndex(0);<br><br> // Setup projection transform to WGS84<br> SpatialReference srcProjection = shpLayer.GetSpatialRef();<br> SpatialReference destProjection = new SpatialReference("");<br> destProjection.ImportFromEPSG(4326);<br> CoordinateTransform transform = new CoordinateTransformation(srcProjection, destProjection);<br><br> // Copy layer to geo json<br> Driver jsonDriver = Ogr.GetDriverByName("GeoJSON");<br> DataSource jsonDataSource = jsonDriver.CreateDataSource(tempJsonFilename, new string[] { });<br> Layer jsonLayer = jsonDataSource.CreateLayer(shpLayer.GetName(), destProjection, shpLayer.GetGeomType(), new string[] { });<br><br> Feature shpFeature = shpLayer.GetNextFeature();<br> while (shpFeature != null)<br> {<br> // Transform geometry<br> Geometry geometry = shpFeature.GetGeometryRef();<br> geometry.Transform(transform);<br><br> // Save feature to new layer<br> jsonLayer.CreateFeature(shpFeature);<br><br> shpFeature = shpLayer.GetNextFeature();<br> }<br><br> IntPtr buffer = Gdal.VSIFOpenL(tempJsonFilename, {some Mode value});<br></i></div></div></blockquote></div></div></blockquote><div> string result = Marshall.PtrToStringAnsi(buffer);</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><i><br> // Close all GDAL stuff<br> jsonLayer.Dispose();<br> jsonDataSource.Dispose();<br> jsonDriver.Dispose();<br> shpLayer.Dispose();<br> shpDatasource.Dispose();<br> shpDriver.Dispose();<br><br> // Read json from buffer<br> var jsonText = Encoding.UTF8.GetString(jsonBuffer, 0, jsonBuffer.Length); ;<br> var json = JsonConvert.DeserializeObject<FeatureCollection>(jsonText);<br><br> return json;<br> }<br> catch (Exception exception)<br> {<br> throw exception;<br> }<br> finally<br> {<br> // Clear files from GDAL<br> Gdal.Unlink(tempJsonFilename);<br> foreach (var file in files)<br> {<br> Gdal.Unlink($"/vsimem/{file.Filename}");<br> }<br> }<br> }</i><br></div><div><br></div><div><br></div><div>Best Regards</div><font color="#888888"><div>Christian</div></font><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><br><div><div style="font-size:13px"><b><font face="georgia, serif">Christian Sörensen</font></b></div><div><b><font face="georgia, serif" size="1">UX Productions AB</font></b></div><div style="color:rgb(136,136,136);font-size:13px"><div><font face="trebuchet ms, sans-serif"><font size="1"><a href="http://www.uxproductions.se" target="_blank">www.uxproductions.se</a> | <a href="mailto:christian@uxproductions.se" style="color:rgb(17,85,204)" target="_blank">christian@uxproductions.se</a> | </font><span style="font-size:x-small"><a value="+46702677212" style="color:rgb(17,85,204)">+46 (0)70 26 77 212</a> </span></font></div><div><span lang="EN-GB" style="font-size:13.5pt;font-family:Webdings;color:green">P</span><span lang="EN-GB" style="font-size:11.5pt;font-family:Calibri,sans-serif;color:black"> </span><span style="font-size:7.5pt;font-family:Arial,sans-serif;color:green">Överväg miljöpåverkan innan du skriver ut detta e-postmeddelande</span></div></div></div></div></div></div>
_______________________________________________<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="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a></blockquote></div></div>
</blockquote></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><br><div><div style="font-size:13px"><b><font face="georgia, serif">Christian Sörensen</font></b></div><div><b><font face="georgia, serif" size="1">UX Productions AB</font></b></div><div style="color:rgb(136,136,136);font-size:13px"><div><font face="trebuchet ms, sans-serif"><font size="1"><a href="http://www.uxproductions.se" target="_blank">www.uxproductions.se</a> | <a href="mailto:christian@uxproductions.se" style="color:rgb(17,85,204)" target="_blank">christian@uxproductions.se</a> | </font><span style="font-size:x-small"><a value="+46702677212" style="color:rgb(17,85,204)">+46 (0)70 26 77 212</a> </span></font></div><div><span lang="EN-GB" style="font-size:13.5pt;font-family:Webdings;color:green">P</span><span lang="EN-GB" style="font-size:11.5pt;font-family:Calibri,sans-serif;color:black"> </span><span style="font-size:7.5pt;font-family:Arial,sans-serif;color:green">Överväg miljöpåverkan innan du skriver ut detta e-postmeddelande</span></div></div></div></div></div>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><br><div><div style="font-size:13px"><b><font face="georgia, serif">Christian Sörensen</font></b></div><div><b><font face="georgia, serif" size="1">UX Productions AB</font></b></div><div style="color:rgb(136,136,136);font-size:13px"><div><font face="trebuchet ms, sans-serif"><font size="1"><a href="http://www.uxproductions.se" target="_blank">www.uxproductions.se</a> | <a href="mailto:christian@uxproductions.se" style="color:rgb(17,85,204)" target="_blank">christian@uxproductions.se</a> | </font><span style="font-size:x-small"><a value="+46702677212" style="color:rgb(17,85,204)">+46 (0)70 26 77 212</a> </span></font></div><div><span lang="EN-GB" style="font-size:13.5pt;font-family:Webdings;color:green">P</span><span lang="EN-GB" style="font-size:11.5pt;font-family:Calibri,sans-serif;color:black"> </span><span style="font-size:7.5pt;font-family:Arial,sans-serif;color:green">Överväg miljöpåverkan innan du skriver ut detta e-postmeddelande</span></div></div></div></div></div>
</blockquote></div></div>