|
10 | 10 | using System.Text;
|
11 | 11 | using System.Text.Json;
|
12 | 12 | using System.Text.Json.Serialization;
|
| 13 | +using OSGeo.OGR; |
| 14 | +using MaxRev.Gdal.Core; |
13 | 15 |
|
14 | 16 | namespace BDMS.Controllers;
|
15 | 17 |
|
@@ -151,6 +153,82 @@ public async Task<ActionResult> ExportJsonAsync([FromQuery][MaxLength(MaxPageSiz
|
151 | 153 | // Add special converter for the 'Observations' collection
|
152 | 154 | options.Converters.Add(new ObservationConverter());
|
153 | 155 |
|
| 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 | + |
154 | 232 | return new JsonResult(boreholes, options);
|
155 | 233 | }
|
156 | 234 |
|
|
0 commit comments