[gdal-dev] Convert ESRI Shapefile to GeoJSON in memory, code in c#
Paul Harwood
runette at gmail.com
Wed Nov 11 06:27:37 PST 2020
Just realised that I excluded the wider from the group accidentally - so
added them back in .
> 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.
Makes sense.
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.
But ..
I found this earlier today and got the same exception as him at the
> Marshal.Copy line (the Python version uses VSIFReadL).
> https://lists.osgeo.org/pipermail/gdal-dev/2018-July/048838.html
>
> * 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);*
>
>
Could that be because you are closing the buffer with
`Gdal.VSIFCloseL(bufPtr)`? Why close it before you read it?
Did you get a valid pointer before closing it? What was the value of size?
>
>
>
>
>
> Den ons 11 nov. 2020 kl 14:44 skrev Paul Harwood <runette at gmail.com>:
>
>> The MongoDB driver is definitely the way to go if you can :)
>>
>> Or - since you seem to have a FeatureCollection class in the last line -
>> just right the Features directly to that?
>>
>> 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?
>>
>> On Wed, 11 Nov 2020 at 12:36, Christian Sörensen <
>> christian at uxproductions.se> wrote:
>>
>>> 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.
>>>
>>> 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.
>>>
>>> Den ons 11 nov. 2020 kl 12:55 skrev Paul Harwood <runette at gmail.com>:
>>>
>>>> Customers! What can you say about Customers! :) Seems the worst of all
>>>> possible worlds to me - but who are we to argue :)
>>>>
>>>> However - there does appear to be `IntPtr Gdal.VSIFOpenL(utf8_path,
>>>> pszMode )`
>>>>
>>>> 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:
>>>>
>>>> I have no guarantee that this would work or provide anything you want.
>>>>
>>>> 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
>>>> https://gdal.org/drivers/vector/mongodbv3.html#vector-mongodbv3 won't
>>>> do the job?
>>>>
>>>> using System.Runtime.InteropServices;
>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> * private FeatureCollection ImportShapeFile(IEnumerable<ImportFile>
>>>>>> files) { var tempJsonFilename = $"/vsimem/temp.json";
>>>>>> GdalConfiguration.ConfigureGdal();
>>>>>> GdalConfiguration.ConfigureOgr(); try {*
>>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> * // Load shape Driver shpDriver =
>>>>>> Ogr.GetDriverByName("ESRI Shapefile"); var shpFile =
>>>>>> files.First(f => f.Filename.EndsWith(".shp")); Datasource
>>>>>> shpDatasource = Ogr.Open($"/vsimem/{shpFile.Filename}", 0);
>>>>>> if (shpDatasource == null) return null;
>>>>>> Layer shpLayer = shpDatasource.GetLayerByIndex(0); // Setup
>>>>>> projection transform to WGS84 SpatialReference srcProjection
>>>>>> = shpLayer.GetSpatialRef(); SpatialReference destProjection
>>>>>> = new SpatialReference("");
>>>>>> destProjection.ImportFromEPSG(4326); CoordinateTransform
>>>>>> transform = new CoordinateTransformation(srcProjection, destProjection);
>>>>>> // Copy layer to geo json Driver jsonDriver =
>>>>>> Ogr.GetDriverByName("GeoJSON"); DataSource jsonDataSource =
>>>>>> jsonDriver.CreateDataSource(tempJsonFilename, new string[] { });
>>>>>> Layer jsonLayer = jsonDataSource.CreateLayer(shpLayer.GetName(),
>>>>>> destProjection, shpLayer.GetGeomType(), new string[] { });
>>>>>> Feature shpFeature = shpLayer.GetNextFeature(); while
>>>>>> (shpFeature != null) { // Transform
>>>>>> geometry Geometry geometry =
>>>>>> shpFeature.GetGeometryRef();
>>>>>> geometry.Transform(transform); // Save feature to new
>>>>>> layer jsonLayer.CreateFeature(shpFeature);
>>>>>> shpFeature = shpLayer.GetNextFeature(); }
>>>>>> IntPtr buffer = Gdal.VSIFOpenL(tempJsonFilename, {some Mode value});*
>>>>>>
>>>>> string result = Marshall.PtrToStringAnsi(buffer);
>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> * // Close all GDAL stuff
>>>>>> jsonLayer.Dispose(); jsonDataSource.Dispose();
>>>>>> jsonDriver.Dispose(); shpLayer.Dispose();
>>>>>> shpDatasource.Dispose(); shpDriver.Dispose();
>>>>>> // Read json from buffer var jsonText =
>>>>>> Encoding.UTF8.GetString(jsonBuffer, 0, jsonBuffer.Length); ;
>>>>>> var json = JsonConvert.DeserializeObject<FeatureCollection>(jsonText);
>>>>>> return json; } catch (Exception exception)
>>>>>> { throw exception; }
>>>>>> finally { // Clear files from GDAL
>>>>>> Gdal.Unlink(tempJsonFilename); foreach (var file in files)
>>>>>> {
>>>>>> Gdal.Unlink($"/vsimem/{file.Filename}"); } }
>>>>>> }*
>>>>>>
>>>>>>
>>>>>> Best Regards
>>>>>> Christian
>>>>>>
>>>>>> --
>>>>>>
>>>>>> *Christian Sörensen*
>>>>>> *UX Productions AB*
>>>>>> www.uxproductions.se | christian at uxproductions.se | +46 (0)70 26 77
>>>>>> 212
>>>>>> P Ãverväg miljöpÃ¥verkan innan du skriver ut detta e-postmeddelande
>>>>>> _______________________________________________
>>>>>> gdal-dev mailing list
>>>>>> gdal-dev at lists.osgeo.org
>>>>>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>>>>>
>>>>>
>>>
>>> --
>>>
>>> *Christian Sörensen*
>>> *UX Productions AB*
>>> www.uxproductions.se | christian at uxproductions.se | +46 (0)70 26 77 212
>>> P Ãverväg miljöpÃ¥verkan innan du skriver ut detta e-postmeddelande
>>>
>>
>
> --
>
> *Christian Sörensen*
> *UX Productions AB*
> www.uxproductions.se | christian at uxproductions.se | +46 (0)70 26 77 212
> P Ãverväg miljöpÃ¥verkan innan du skriver ut detta e-postmeddelande
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201111/cd3681f5/attachment-0001.html>
More information about the gdal-dev
mailing list