[gdal-dev] Convert ESRI Shapefile to GeoJSON in memory, code in c#
Christian Sörensen
christian at uxproductions.se
Wed Nov 11 02:19:47 PST 2020
Hi,
I'm a bit out of my depth here trying out GDAL in c#. Please help. =)
My code works fine if I output the GeoJSON to a file on disk. But I would
really not prefer having to intermittently store the result on disk just to
read it back into memory again.
It seems like all methods exposed by SWIG in c# that have something to do
with reading virtual files back into a buffer is missing?
Where is VSIGetMemFileBuffer or VSIFReadL? Python seems to have VSIFReadL,
but not C#.
I'm doing this wrong?
Below is my code.
* 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201111/e4737718/attachment-0001.html>
More information about the gdal-dev
mailing list