[gdal-dev] Convert ESRI Shapefile to GeoJSON in memory, code in c#
Paul Harwood
runette at gmail.com
Wed Nov 11 03:34:30 PST 2020
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201111/a77a0e10/attachment.html>
More information about the gdal-dev
mailing list