Skip to content

Commit fe00f3f

Browse files
committed
Convert boreholes to geojson and geopackage
1 parent 6848140 commit fe00f3f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/api/BDMS.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<PackageReference Include="CsvHelper" Version="30.0.1" />
1212
<PackageReference Include="EFCore.BulkExtensions.PostgreSql" Version="8.0.0" />
1313
<PackageReference Include="Humanizer" Version="2.14.1" />
14+
<PackageReference Include="MaxRev.Gdal.Core" Version="3.10.0.306" />
15+
<PackageReference Include="MaxRev.Gdal.WindowsRuntime.Minimal" Version="3.10.0.306" />
1416
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
1517
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
1618
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.0" />

src/api/Controllers/BoreholeController.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using System.Text;
1111
using System.Text.Json;
1212
using System.Text.Json.Serialization;
13+
using OSGeo.OGR;
14+
using MaxRev.Gdal.Core;
1315

1416
namespace BDMS.Controllers;
1517

@@ -151,6 +153,82 @@ public async Task<ActionResult> ExportJsonAsync([FromQuery][MaxLength(MaxPageSiz
151153
// Add special converter for the 'Observations' collection
152154
options.Converters.Add(new ObservationConverter());
153155

156+
try
157+
{
158+
var features = boreholes.Select(borehole =>
159+
{
160+
161+
var feature = new
162+
{
163+
type = "Feature",
164+
geometry = new
165+
{
166+
type = "Point",
167+
crs = new
168+
{
169+
type = "name",
170+
properties = new
171+
{
172+
name = "EPSG:2056",
173+
},
174+
},
175+
coordinates = new[]
176+
{
177+
borehole.LocationX,
178+
borehole.LocationY,
179+
},
180+
},
181+
properties = borehole,
182+
};
183+
return feature;
184+
}).ToList();
185+
186+
var geojson = new
187+
{
188+
type = "FeatureCollection",
189+
crs = new
190+
{
191+
type = "name",
192+
properties = new
193+
{
194+
name = "EPSG:2056",
195+
},
196+
},
197+
features,
198+
};
199+
200+
var geojsonFile = "bulkexport_test.geojson";
201+
await System.IO.File.WriteAllTextAsync(geojsonFile, JsonSerializer.Serialize(geojson, options)).ConfigureAwait(false);
202+
203+
GdalBase.ConfigureAll();
204+
Ogr.RegisterAll();
205+
Ogr.UseExceptions();
206+
207+
var geojsonDataSource = Ogr.Open(geojsonFile, 1);
208+
if (geojsonDataSource == null)
209+
{
210+
throw new InvalidOperationException("Could not open input datasource.");
211+
}
212+
213+
var gpkgFilePath = geojsonFile.Replace(".geojson", ".gpkg", StringComparison.InvariantCulture);
214+
if (Directory.Exists(gpkgFilePath))
215+
{
216+
Directory.Delete(gpkgFilePath, true);
217+
}
218+
219+
var openFileGdbDriver = Ogr.GetDriverByName("GPKG");
220+
var gpkgDataSource = openFileGdbDriver.CreateDataSource(gpkgFilePath, null);
221+
222+
gpkgDataSource.CopyLayer(geojsonDataSource.GetLayerByIndex(0), "boreholes", null);
223+
224+
geojsonDataSource.Dispose();
225+
gpkgDataSource.Dispose();
226+
}
227+
catch (Exception e)
228+
{
229+
Logger.LogError(e.Message);
230+
}
231+
154232
return new JsonResult(boreholes, options);
155233
}
156234

0 commit comments

Comments
 (0)