[gdal-dev] Convert ESRI Shapefile to GeoJSON in memory, code in c#

Christian Sörensen christian at uxproductions.se
Wed Nov 11 03:42:20 PST 2020


After converting to GeoJSON the next step is saving the json into a MongoDB
database. The customer I work for has chosen to store all GIS-data in
GeoJSON in a nosql database like MongoDB.

That's why I would prefer not creating a file just to get hold of the json
after importing and transforming the original data using GDAL. In the
future we're thinking of supporting more formats than ESRI Shapefile and
GeoJSON as input.

Hope this explains my question.

Den ons 11 nov. 2020 kl 12:34 skrev Paul Harwood <runette at gmail.com>:

> I have a working c# GDAL app - I might be able to help.
>
> I am not sure I understand what you are trying to do? It looks like you
> are creating a GeoJSON file from a shapefile and that actually only makes
> any sense if you want to save the file?
>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> * private FeatureCollection ImportShapeFile(IEnumerable<ImportFile>
>> files)        {            var tempJsonFilename = $"/vsimem/temp.json";
>>         GdalConfiguration.ConfigureGdal();
>> GdalConfiguration.ConfigureOgr();            try            {
>>   // Copy into GDAL                foreach (var file in files)
>>   {                    Gdal.FileFromMemBuffer($"/vsimem/{file.Filename}",
>> file.Data);                }*
>>
>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> *                // Load shape                var shpDriver =
>> Ogr.GetDriverByName("ESRI Shapefile");                var shpFile =
>> files.First(f => f.Filename.EndsWith(".shp"));                var
>> shpDatasource = Ogr.Open($"/vsimem/{shpFile.Filename}", 0);
>> if (shpDatasource == null)                    return null;
>> var shpLayer = shpDatasource.GetLayerByIndex(0);                // Setup
>> projection transform to WGS84                var srcProjection =
>> shpLayer.GetSpatialRef();                var destProjection = new
>> SpatialReference("");                destProjection.ImportFromEPSG(4326);
>>               var transform = new CoordinateTransformation(srcProjection,
>> destProjection);                // Copy layer to geo json
>> var jsonDriver = Ogr.GetDriverByName("GeoJSON");                var
>> jsonDataSource = jsonDriver.CreateDataSource(tempJsonFilename, new string[]
>> { });                var jsonLayer =
>> jsonDataSource.CreateLayer(shpLayer.GetName(), destProjection,
>> shpLayer.GetGeomType(), new string[] { });                var shpFeature =
>> shpLayer.GetNextFeature();                while (shpFeature != null)
>>         {                    // Transform geometry                    var
>> geometry = shpFeature.GetGeometryRef();
>> geometry.Transform(transform);                    // Save feature to new
>> layer                    jsonLayer.CreateFeature(shpFeature);
>>       shpFeature = shpLayer.GetNextFeature();                }
>>   // TODO: HERE I would like to read the information from my tempJsonFile
>> back into a byte array, before unlinking.                var jsonBuffer =
>> new byte[0];                // 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201111/69ab5d00/attachment-0001.html>


More information about the gdal-dev mailing list