public void OsmAllWithTerrain(string name, string bboxWKT, bool centerOnOrigin, float ZScale) { DEMDataSet dataset = DEMDataSet.NASADEM; var bbox = GeometryService.GetBoundingBox(bboxWKT); bool computeElevations = false; string outputDir = Directory.GetCurrentDirectory(); var transform = new ModelGenerationTransform(bbox, Reprojection.SRID_PROJECTED_MERCATOR, centerOnOrigin: centerOnOrigin, ZScale, centerOnZOrigin: false); var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); transform.BoundingBox = bbox; // bbox changed by GetHeigtMap heightMap = transform.TransformHeightMap(heightMap); TileRange tiles = _imageryService.DownloadTiles(bbox, ImageryProvider.EsriWorldImagery, 15); string fileName = Path.Combine(outputDir, "Texture.jpg"); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); var pbrTexture = PBRTexture.Create(texInfo, null); ModelRoot model = _osmProcessor.Run(null, OsmLayer.Buildings | OsmLayer.Highways, bbox, transform, computeElevations, dataset, downloadMissingFiles: true); model = _gltfService.AddTerrainMesh(model, heightMap, pbrTexture); model.SaveGLB(Path.Combine(outputDir, $"{nameof(OsmAllWithTerrain)}_{name}" + ".glb")); }
public void Run() { try { DEMDataSet dataset = DEMDataSet.AW3D30; ImageryProvider imageryProvider = ImageryProvider.MapBoxSatellite; Stopwatch sw = Stopwatch.StartNew(); string modelName = $"Montagne Sainte Victoire {dataset.Name}"; string outputDir = Directory.GetCurrentDirectory(); // You can get your boox from https://geojson.net/ (save as WKT) string bboxWKT = "POLYGON((5.54888 43.519525, 5.61209 43.519525, 5.61209 43.565225, 5.54888 43.565225, 5.54888 43.519525))"; _logger.LogInformation($"Processing model {modelName}..."); _logger.LogInformation($"Getting bounding box geometry..."); var bbox = GeometryService.GetBoundingBox(bboxWKT); _logger.LogInformation($"Getting height map data..."); var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); _logger.LogInformation($"Processing height map data ({heightMap.Count} coordinates)..."); heightMap = heightMap .ReprojectGeodeticToCartesian() // Reproject to 3857 (useful to get coordinates in meters) .ZScale(2f) // Elevation exageration .CenterOnOrigin(); TileRange tiles = _imageryService.DownloadTiles(bbox, imageryProvider, 8); var texture = _imageryService.ConstructTexture(tiles, bbox, Path.Combine(outputDir, modelName + "_texture.jpg"), TextureImageFormat.image_jpeg); var normalMap = _imageryService.GenerateNormalMap(heightMap, outputDir, modelName + "_normalmap.png"); var pbrTexture = PBRTexture.Create(texture, normalMap); // Triangulate height map _logger.LogInformation($"Triangulating height map and generating 3D mesh..."); var model = _sharpGltfService.CreateTerrainMesh(heightMap, pbrTexture); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + ".glb")); _logger.LogInformation($"Done in {sw.Elapsed:g}"); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }
internal void ExportVisualTopoToGLB(VisualTopoModel visualTopoModel, DEMDataSet dataset, string outputFile, bool drawOnTexture, float marginMeters, float zFactor, ImageryProvider imageryProvider) { int outputSrid = 3857; float lineWidth = 1.0F; int numTilesPerImage = 4; BoundingBox bbox = visualTopoModel.BoundingBox // relative coords .Translate(visualTopoModel.EntryPoint.Longitude, visualTopoModel.EntryPoint.Latitude, visualTopoModel.EntryPoint.Elevation ?? 0) // absolute coords .Pad(marginMeters) // margin around model .ReprojectTo(visualTopoModel.SRID, dataset.SRID); // DEM coords //======================= // Height map (get dem elevation for bbox) // // Get height map // Note that ref Bbox means that the bbox will be adjusted to match DEM data var heightMap = elevationService.GetHeightMap(ref bbox, dataset, downloadMissingFiles: true); var bboxTerrainSpace = bbox.ReprojectTo(dataset.SRID, outputSrid); // terrain coords // Model origin GeoPoint axisOriginWorldSpace = visualTopoModel.EntryPoint.ReprojectTo(visualTopoModel.SRID, outputSrid) .CenterOnOrigin(bboxTerrainSpace); Vector3 axisOriginModelSpace = visualTopoModel.EntryPoint.AsVector3(); //await signalR.ReportGenerateProgressAsync(signalRConnectionId, "Getting GPX extent elevation...", 20); //======================= // Local transform function from model coordinates (relative to entry, in meters) // and global coordinates absolute in final 3D model space // IEnumerable <GeoPoint> TransformLine(IEnumerable <GeoPoint> line) { var newLine = line.Translate(visualTopoModel.EntryPoint) // Translate to entry (=> global topo coord space) .ReprojectTo(visualTopoModel.SRID, outputSrid) // Reproject to terrain coord space .CenterOnOrigin(bboxTerrainSpace) // Center on terrain space origin .CenterOnOrigin(axisOriginWorldSpace); return(newLine); }; //======================= // 3D model // var gltfModel = sharpGltfService.CreateNewModel(); // Add X/Y/Z axis on entry point var axis = meshService.CreateAxis(); sharpGltfService.AddMesh(gltfModel, "Axis", axis, doubleSided: false); int i = 0; var triangulation = visualTopoModel.TriangulationFull3D.Clone() .Translate(axisOriginModelSpace) // already zScaled if zFactor > 1 .ReprojectTo(visualTopoModel.SRID, outputSrid) .CenterOnOrigin(bboxTerrainSpace) .CenterOnOrigin(axisOriginWorldSpace.AsVector3()); gltfModel = sharpGltfService.AddMesh(gltfModel, "Cavite3D", triangulation, VectorsExtensions.CreateColor(0, 255, 0), doubleSided: false); ////======================= //// 3D cavity model //foreach (var line in visualTopoModel.Topology3D) // model.Topology3D is the graph of topo paths //{ // // Add line to model // gltfModel = sharpGltfService.AddLine(gltfModel // , string.Concat("CavitySection", i++) // name of 3D node // , TransformLine(line) // call transform function // , color: VectorsExtensions.CreateColor(255, 0, 0, 128) // , lineWidth); //} // Reproject and center height map coordinates heightMap = heightMap.ReprojectTo(dataset.SRID, outputSrid) .CenterOnOrigin(bboxTerrainSpace) .ZScale(zFactor) .CenterOnOrigin(axisOriginWorldSpace); //======================= // Textures // PBRTexture pbrTexture = null; if (imageryProvider != null) { TileRange tiles = imageryService.DownloadTiles(bbox, imageryProvider, numTilesPerImage); string fileName = Path.Combine(Path.GetDirectoryName(outputFile), "Texture.jpg"); Console.WriteLine("Construct texture..."); TextureInfo texInfo = null; if (drawOnTexture) { var topoTexture = visualTopoModel.Topology3D.SelectMany(l => l).Translate(visualTopoModel.EntryPoint).ReprojectTo(visualTopoModel.SRID, 4326); texInfo = imageryService.ConstructTextureWithGpxTrack(tiles, bbox, fileName, TextureImageFormat.image_jpeg , topoTexture, drawGpxVertices: true); } else { texInfo = imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); } pbrTexture = PBRTexture.Create(texInfo, null); } // //======================= gltfModel = sharpGltfService.AddTerrainMesh(gltfModel, heightMap, pbrTexture); gltfModel.SaveGLB(outputFile); }
internal void Run(ServiceProvider serviceProvider) { bool useTIN = false; // still buggy with SRID 3857 int v_outSrid = Reprojection.SRID_PROJECTED_MERCATOR; IglTFService glTF = serviceProvider.GetService <IglTFService>(); IElevationService elevationService = serviceProvider.GetService <IElevationService>(); string outputDir = Path.GetFullPath(Path.Combine(_outputDirectory, "glTF")); Logger.Info("============================"); Logger.Info($"= {nameof(TextureSamples)}"); Logger.Info("============================"); Logger.Info($"= {nameof(TextureSamples)} : Datadirectory report"); // Get GPX points var bbox = GeometryService.GetBoundingBox(_bboxWkt); //======================= // Textures // TextureInfo texInfo = null; ImageryService imageryService = new ImageryService(); Console.WriteLine("Download image tiles..."); TileRange tiles = imageryService.DownloadTiles(bbox, ImageryProvider.StamenToner, 1); Console.WriteLine("Construct texture..."); string fileName = Path.Combine(outputDir, "Texture.jpg"); texInfo = imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); // //======================= //======================= // Normal map Console.WriteLine("Height map..."); float Z_FACTOR = 2f; HeightMap hMapNormal = elevationService.GetHeightMap(bbox, _normalsDataSet); //HeightMap hMapNormal = _elevationService.GetHeightMap(bbox, Path.Combine(_localdatadir, "ETOPO1", "ETOPO1_Bed_g_geotiff.tif"), DEMFileFormat.GEOTIFF); hMapNormal = hMapNormal.ReprojectTo(4326, v_outSrid); //hMapNormal = hMapNormal.ReprojectGeodeticToCartesian(); Console.WriteLine("Generate normal map..."); TextureInfo normal = imageryService.GenerateNormalMap(hMapNormal, outputDir); // //======================= //======================= // Get height map HeightMap hMap = elevationService.GetHeightMap(bbox, _meshDataSet); //HeightMap hMap = _elevationService.GetHeightMap(bbox, Path.Combine(_localdatadir, "ETOPO1","ETOPO1_Bed_g_geotiff.tif"), DEMFileFormat.GEOTIFF); //======================= // UV mapping (before projection) PBRTexture pBRTexture = PBRTexture.Create(texInfo, normal, imageryService.ComputeUVMap(hMap, texInfo)); hMap = hMap.ReprojectTo(4326, v_outSrid); hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); //======================= //======================= // MESH 3D terrain List <MeshPrimitive> meshes = new List <MeshPrimitive>(); // generate mesh with texture MeshPrimitive triangleMesh; if (useTIN) { Console.WriteLine("Create TIN..."); //triangleMesh = GenerateTIN(hMapTIN, glTF, pBRTexture); triangleMesh = TINGeneration.GenerateTIN(hMap, 10d, glTF, pBRTexture, v_outSrid); } else { Console.WriteLine("GenerateTriangleMesh..."); triangleMesh = glTF.GenerateTriangleMesh_Boxed(hMap); } meshes.Add(triangleMesh); // model export Console.WriteLine("GenerateModel..."); Model model = glTF.GenerateModel(meshes, this.GetType().Name); glTF.Export(model, outputDir, $"{GetType().Name} NONormal", false, true); }
public void GenerateModel(string bboxWkt, DEMDataSet litto3DDataset, ImageryProvider imageryProvider, float zFactor = 3f, bool withTexture = true, bool withboat = true, bool withWaterSurface = true, DEMDataSet fallbackDataset = null) { try { bool centerOnOrigin = true; int TEXTURE_TILES = 20; string outputDir = Directory.GetCurrentDirectory(); string modelName = $"{litto3DDataset.ResolutionMeters}m_z{zFactor}_{imageryProvider.Name}-{DateTime.Now:yyyyMMdd-hhmmss}"; _logger.LogInformation($"Getting height map data..."); var bbox = GeometryService.GetBoundingBox(bboxWkt).ReprojectTo(4326, litto3DDataset.SRID); var heightMap = _elevationService.GetHeightMap(ref bbox, litto3DDataset); heightMap = heightMap.BakeCoordinates(); var nullCoordsEnumerator = heightMap.Coordinates.Where(c => (c.Elevation ?? litto3DDataset.NoDataValue) == litto3DDataset.NoDataValue); var interpolator = _elevationService.GetInterpolator(InterpolationMode.Bilinear); using (IRasterFile raster = _rasterService.OpenFile(@"D:\Data\ELEVATION_DO_NOT_DELETE\GEBCO2020\cote azur\gebco_2020_subset.tif", DEMFileType.GEOTIFF)) using (RasterFileDictionary dic = new RasterFileDictionary()) { var metadata = raster.ParseMetaData(fallbackDataset.FileFormat); dic.Add(metadata, raster); foreach (var pt in nullCoordsEnumerator) { var proj = pt.ReprojectTo(litto3DDataset.SRID, fallbackDataset.SRID); pt.Elevation = _elevationService.GetElevationAtPoint(raster, dic, metadata, proj.Latitude, proj.Longitude, 0, interpolator, NoDataBehavior.UseNoDataDefinedInDem); } } var nullCoords = heightMap.Coordinates.Where(c => (c.Elevation ?? litto3DDataset.NoDataValue) == litto3DDataset.NoDataValue).ToList(); var nullCoordsFallbackProj = nullCoords.ReprojectTo(litto3DDataset.SRID, fallbackDataset.SRID, nullCoords.Count); var nullCoordsFallbackProj2 = _elevationService.GetPointsElevation(nullCoordsFallbackProj, fallbackDataset, InterpolationMode.Bilinear, NoDataBehavior.UseNoDataDefinedInDem); ModelGenerationTransform transform = new ModelGenerationTransform(bbox, 3857, centerOnOrigin: centerOnOrigin, zFactor, centerOnZOrigin: false); ModelGenerationTransform transformFrom4326 = new ModelGenerationTransform(bbox.ReprojectTo(2154, 4326), 3857, centerOnOrigin: centerOnOrigin, zFactor, centerOnZOrigin: false); _logger.LogInformation($"Processing height map data ({heightMap.Count} coordinates)..."); heightMap = transform.TransformHeightMap(heightMap); var min = heightMap.Coordinates.Where(g => (g.Elevation ?? 0d) > litto3DDataset.NoDataValue).Min(g => g.Elevation ?? 0d); heightMap.Coordinates = heightMap.Coordinates.Select(p => { if (p.Elevation.GetValueOrDefault(0) <= litto3DDataset.NoDataValue) { p.Elevation = min; } return(p); }); //======================= // Textures // PBRTexture pbrTexture = null; if (withTexture) { var bbox4326 = bbox.ReprojectTo(2154, 4326); Console.WriteLine("Download image tiles..."); TileRange tiles = _imageryService.DownloadTiles(bbox4326, imageryProvider, TEXTURE_TILES); string fileName = Path.Combine(outputDir, "Texture.jpg"); Console.WriteLine("Construct texture..."); //TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox4326, fileName, TextureImageFormat.image_jpeg); TextureInfo texInfo; if (withboat) { var trackPoints = GetGeoPointFromGeoJson(BoatCourseGeoJson); texInfo = _imageryService.ConstructTextureWithGpxTrack(tiles, bbox4326, fileName, TextureImageFormat.image_jpeg, trackPoints, drawGpxVertices: true, color: SixLabors.ImageSharp.PixelFormats.Rgba32.Green, 30); } else { texInfo = _imageryService.ConstructTexture(tiles, bbox4326, fileName, TextureImageFormat.image_jpeg); } // //======================= //======================= // Normal map Console.WriteLine("Height map..."); //float Z_FACTOR = 0.00002f; //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); //var normalMap = _imageryService.GenerateNormalMap(heightMap, outputDir); pbrTexture = PBRTexture.Create(texInfo, null);// normalMap); //hMap = hMap.CenterOnOrigin(Z_FACTOR); // //======================= } // Triangulate height map // and add base and sides _logger.LogInformation($"Triangulating height map and generating 3D mesh..."); heightMap = heightMap.BakeCoordinates(); var coords = heightMap.Coordinates.ToList(); var model = _sharpGltfService.CreateTerrainMesh(heightMap, pbrTexture); if (withWaterSurface) { var bottomLeft = coords[heightMap.Width * (heightMap.Height - 1)].AsVector3(); bottomLeft.Z = 0; var topRight = coords[heightMap.Width - 1].AsVector3(); topRight.Z = 0; var topLeft = coords[0].AsVector3(); topLeft.Z = 0; var bottomRight = coords.Last().AsVector3(); bottomRight.Z = 0; var waterSurface = _meshService.CreateWaterSurface(bottomLeft, topRight, topLeft, bottomRight, minZ: (float)min, color: VectorsExtensions.CreateColor(0, 150, 255, 64)); model = _sharpGltfService.AddMesh(model, "Water", waterSurface, doubleSided: true); } if (withboat) { var boatInitPos = centerOnOrigin ? new GeoPoint(0, 0).AsVector3() : new GeoPoint(43.010625204304304, 6.3711613671060086).ReprojectTo(4326, 3857).AsVector3(); var axis = _meshService.CreateAxis(2, 10, 3, 3).Translate(boatInitPos); model = _sharpGltfService.AddMesh(model, "Boat", axis, doubleSided: false); var boatCourse = transformFrom4326.TransformPoints(GetGeoPointFromGeoJson(BoatCourseGeoJson)).ToList(); //ReprojectGeodeticToCartesian().CenterOnOrigin().ToList(); model = _sharpGltfService.AddLine(model, "BoatCourse", boatCourse, VectorsExtensions.CreateColor(255, 0, 0, 128), 4); } model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + ".glb")); _logger.LogInformation($"Model exported as {Path.Combine(Directory.GetCurrentDirectory(), modelName + ".gltf")} and .glb"); //var point = new GeoPoint(43.01142119356318, 6.385200681010872).ReprojectTo(4326, 2154); //point = _elevationService.GetPointElevation(point, litto3DDataset); } catch (Exception e) { _logger.LogError(e, e.Message); } }
private void Buildings3DOnly() { string outputDir = Directory.GetCurrentDirectory(); string WKT_SF_BIG = "POLYGON((-122.53517427420718 37.81548554152065,-122.35149660086734 37.81548554152065,-122.35149660086734 37.70311455416941,-122.53517427420718 37.70311455416941,-122.53517427420718 37.81548554152065))"; string WKT_SF_SMALL = "POLYGON((-122.42722692299768 37.81034598808797, -122.38886060524865 37.81034598808797, -122.38886060524865 37.784573673820816, -122.42722692299768 37.784573673820816, -122.42722692299768 37.81034598808797))"; string WKT_SF_SUPERSMALL = "POLYGON((-122.41063177228989 37.80707295150412,-122.40904390455307 37.80707295150412,-122.40904390455307 37.806064225434206,-122.41063177228989 37.806064225434206,-122.41063177228989 37.80707295150412))"; // FULL string WKT_PRIPYAT_FULL = "POLYGON((30.021919548702254 51.41813804241615,30.083030998897566 51.41813804241615,30.083030998897566 51.389438684773985,30.021919548702254 51.389438684773985,30.021919548702254 51.41813804241615))"; // Cas 1 //string WKT_PRIPYAT = "POLYGON((30.05430313958436 51.404795567637294,30.055118531124887 51.404795567637294,30.055118531124887 51.40432372279637,30.05430313958436 51.40432372279637,30.05430313958436 51.404795567637294))"; // cas 2 //string WKT_PRIPYAT = "POLYGON((30.062877280833636 51.40748141189236,30.063456637980853 51.40748141189236,30.063456637980853 51.40716017522757,30.062877280833636 51.40716017522757,30.062877280833636 51.40748141189236))"; // cas 3 //string WKT_PRIPYAT = "POLYGON((30.065251398582948 51.407283441091266,30.066243815918458 51.407283441091266,30.066243815918458 51.40558353075506,30.065251398582948 51.40558353075506,30.065251398582948 51.407283441091266))"; string WKT_PRIPRYAT_SOMEROADS = "POLYGON((30.062684362726948 51.40792711901257,30.064690655070088 51.40792711901257,30.064690655070088 51.40693664170241,30.062684362726948 51.40693664170241,30.062684362726948 51.40792711901257))"; // SF Big: POLYGON((-122.53517427420718 37.81548554152065,-122.35149660086734 37.81548554152065,-122.35149660086734 37.70311455416941,-122.53517427420718 37.70311455416941,-122.53517427420718 37.81548554152065)) // SF Small: POLYGON((-122.41967382241174 37.81034598808797,-122.39761533547326 37.81034598808797,-122.39761533547326 37.79162804294824,-122.41967382241174 37.79162804294824,-122.41967382241174 37.81034598808797)) // Napoli, multi polygon (https://www.openstreetmap.org/relation/8955771) //new BoundingBox(14.364430059744153, 14.365218629194532, 40.78433307340424, 40.785023575175295); string WKT_AIX_FULL = "POLYGON((5.402291662243135 43.565714431347274,5.48056925013376 43.565714431347274,5.48056925013376 43.50797300081391,5.402291662243135 43.50797300081391,5.402291662243135 43.565714431347274))"; string WKT_AIX_WITHTERRAIN = "POLYGON((5.440657648511835 43.55957815383877,5.444434198804804 43.55957815383877,5.444434198804804 43.5579454365131,5.440657648511835 43.5579454365131,5.440657648511835 43.55957815383877))"; string WKT_AIX_SMALLOSMBUG = "POLYGON((5.441805234256467 43.55910060792738,5.442684998813352 43.55910060792738,5.442684998813352 43.55877017799191,5.441805234256467 43.55877017799191,5.441805234256467 43.55910060792738))"; string WKT_MONACO = "POLYGON((7.392147587957001 43.75577569838535,7.4410710803886415 43.75577569838535,7.4410710803886415 43.71757458493263,7.392147587957001 43.71757458493263,7.392147587957001 43.75577569838535))"; string WKT_MONACO_DEBUG = "POLYGON((7.421709439122424 43.73663530909531,7.433961769902453 43.73663530909531,7.433961769902453 43.733007331111345,7.421709439122424 43.733007331111345,7.421709439122424 43.73663530909531))";//"POLYGON((7.426780270757294 43.73870913810349,7.432520198049164 43.73870913810349,7.432520198049164 43.73501926928533,7.426780270757294 43.73501926928533,7.426780270757294 43.73870913810349))"; string WKT_HK = "POLYGON((114.13119740014092 22.360520982593926,114.21050495629326 22.360520982593926,114.21050495629326 22.28874575980822,114.13119740014092 22.28874575980822,114.13119740014092 22.360520982593926))"; string WKT_FRISCO = "POLYGON((-122.5235839391063 37.81433638393927,-122.36222224477036 37.81433638393927,-122.36222224477036 37.71228516909579,-122.5235839391063 37.71228516909579,-122.5235839391063 37.81433638393927))"; string WKT_DEFENSE = "POLYGON((2.222075572216413 48.902615468120246,2.3024130966304757 48.902615468120246,2.3024130966304757 48.86355756505397,2.222075572216413 48.86355756505397,2.222075572216413 48.902615468120246))"; DEMDataSet dataset = DEMDataSet.NASADEM; var name = nameof(WKT_DEFENSE); var bbox = GeometryService.GetBoundingBox(WKT_DEFENSE); bool computeElevations = true; ModelRoot model = _gltfService.CreateNewModel(); bool withTerrain = false; if (withTerrain) { var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); var transform = new ModelGenerationTransform(bbox, Reprojection.SRID_PROJECTED_MERCATOR, centerOnOrigin: true, ZScale, centerOnZOrigin: true); heightMap = transform.TransformHeightMap(heightMap); TileRange tiles = _imageryService.DownloadTiles(bbox, ImageryProvider.MapBoxSatellite, 15); string fileName = Path.Combine(outputDir, "Texture.jpg"); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); var pbrTexture = PBRTexture.Create(texInfo, null); model = _osmProcessor.Run(model, OsmLayer.Buildings | OsmLayer.Highways, bbox, transform, computeElevations, dataset, downloadMissingFiles: true); model = _gltfService.AddTerrainMesh(model, heightMap, pbrTexture); } else { var transform = new ModelGenerationTransform(bbox, Reprojection.SRID_PROJECTED_MERCATOR, centerOnOrigin: true, ZScale, centerOnZOrigin: true); model = _osmProcessor.Run(model, OsmLayer.Buildings | OsmLayer.Highways, bbox, transform, computeElevations, dataset, downloadMissingFiles: true); } model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), name + ".glb")); }
internal void Run(DEMDataSet dataSet, bool trackIn3D = true, bool generateTIN = false, int outputSrid = Reprojection.SRID_PROJECTED_LAMBERT_93) { try { string _gpxFile = Path.Combine("SampleData", "GPX", "lake-pleasant-camping.gpx"); bool withTexture = true; float Z_FACTOR = 1.8f; float Z_TRANSLATE_GPX_TRACK_METERS = 5; float trailWidthMeters = 5f; int skipGpxPointsEvery = 1; ImageryProvider provider = ImageryProvider.MapBoxSatellite; // new TileDebugProvider(null, maxDegreeOfParallelism: 1);// ImageryProvider.MapBoxSatellite; string outputDir = Path.GetFullPath("."); //======================= /// Line strip from GPX /// // Get GPX points var segments = GpxImport.ReadGPX_Segments(_gpxFile); var points = segments.SelectMany(seg => seg); var bbox = points.GetBoundingBox().Scale(1.1, 1.1).ReprojectTo(4326, dataSet.SRID); // DEBUG // Test case : ASTER GDEMv3 : 5.5 43.5 Z=315 // 303 307 308 // 309 *315* 317 // 314 321 324 //points = GenerateDebugTrailPointsGenerateDebugTrailPoints(5.003, 5.006, 43.995, 43.997, 0.0001, 0.001); //points = GenerateDebugTrailPointsGenerateDebugTrailPoints(5.4990, 5.501, 43.4990, 43.501, 0.0001, 0.001); //points = GenerateDebugTrailPointsGenerateDebugTrailPoints(5.49, 5.51, 43.49, 43.51, 0.0005, 0.001); //bbox = points.GetBoundingBox().Scale(1.3,1.3); IEnumerable <GeoPoint> gpxPointsElevated = _elevationService.GetPointsElevation(points, dataSet); // //======================= //======================= /// Height map (get dem elevation for bbox) /// HeightMap hMap = _elevationService.GetHeightMap(ref bbox, dataSet); // var refPoint = new GeoPoint(43.5, 5.5); // hMap = hMap.BakeCoordinates(); // var hMapRefPoint = hMap.Coordinates.OrderBy(c => c.DistanceSquaredTo(refPoint)).First(); // var gpxRefPoint = gpxPointsElevated.OrderBy(c => c.DistanceSquaredTo(refPoint)).First(); // hMapRefPoint.Elevation += 60; // gpxRefPoint.Elevation += 60; hMap = hMap.ReprojectTo(dataSet.SRID, outputSrid) //.CenterOnOrigin() .ZScale(Z_FACTOR) .BakeCoordinates(); // //======================= //======================= // Textures // PBRTexture pbrTexture = null; if (withTexture) { Console.WriteLine("Download image tiles..."); TileRange tiles = _imageryService.DownloadTiles(bbox, provider, 12); string fileName = Path.Combine(outputDir, "Texture.jpg"); Console.WriteLine("Construct texture..."); //TextureInfo texInfo = _imageryService.ConstructTextureWithGpxTrack(tiles, bbox, fileName, TextureImageFormat.image_jpeg, gpxPointsElevated, false); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); // //======================= //======================= // Normal map Console.WriteLine("Height map..."); //float Z_FACTOR = 0.00002f; //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); //var normalMap = _imageryService.GenerateNormalMap(hMap, outputDir); //pbrTexture = PBRTexture.Create(texInfo, normalMap); pbrTexture = PBRTexture.Create(texInfo, null); //hMap = hMap.CenterOnOrigin(Z_FACTOR); // //======================= } //======================= // MESH 3D terrain Console.WriteLine("Height map..."); Console.WriteLine("GenerateTriangleMesh..."); //hMap = _elevationService.GetHeightMap(bbox, _dataSet); ModelRoot model = null; if (generateTIN) { model = TINGeneration.GenerateTIN(hMap, 10d, _sharpGltfService, pbrTexture, outputSrid); } else { //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); // generate mesh with texture model = _sharpGltfService.CreateTerrainMesh(hMap, pbrTexture); } if (trackIn3D) { // take 1 point evert nth gpxPointsElevated = gpxPointsElevated.Where((x, i) => (i + 1) % skipGpxPointsEvery == 0); gpxPointsElevated = gpxPointsElevated.ZTranslate(Z_TRANSLATE_GPX_TRACK_METERS) .ReprojectTo(dataSet.SRID, outputSrid) //.CenterOnOrigin() //.CenterOnOrigin(hMap.BoundingBox) .ZScale(Z_FACTOR); model = _sharpGltfService.AddLine(model, "GPX", gpxPointsElevated, VectorsExtensions.CreateColor(255, 0, 0, 128), trailWidthMeters); } // model export Console.WriteLine("GenerateModel..."); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), $"{GetType().Name} dst{dataSet.Name} TIN{generateTIN} Srid{outputSrid}.glb")); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }
public void Run(DEMDataSet dataset, bool withTexture = true) { try { //_rasterService.GenerateDirectoryMetadata(dataset, false); Stopwatch sw = Stopwatch.StartNew(); string modelName = $"Montagne Sainte Victoire {dataset.Name}"; string outputDir = Directory.GetCurrentDirectory(); ImageryProvider provider = ImageryProvider.MapBoxSatelliteStreet;// new TileDebugProvider(new GeoPoint(43.5,5.5)); // You can get your boox from https://geojson.net/ (save as WKT) string bboxWKT = "POLYGON((5.54888 43.519525, 5.61209 43.519525, 5.61209 43.565225, 5.54888 43.565225, 5.54888 43.519525))"; // string bboxWKT = // "POLYGON((5.594457381483949 43.545276557046044,5.652135604140199 43.545276557046044,5.652135604140199 43.52038635099936,5.594457381483949 43.52038635099936,5.594457381483949 43.545276557046044))"; // _logger.LogInformation($"Processing model {modelName}..."); // // // _logger.LogInformation($"Getting bounding box geometry..."); var bbox = GeometryService.GetBoundingBox(bboxWKT); //var bbox = new BoundingBox(5.5613898348431485,5.597185285307553,43.49372969433046,43.50939068558466); _logger.LogInformation($"Getting height map data..."); var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); _logger.LogInformation($"Processing height map data ({heightMap.Count} coordinates)..."); heightMap = heightMap .ReprojectGeodeticToCartesian() // Reproject to 3857 (useful to get coordinates in meters) .ZScale(2f); // Elevation exageration //======================= // Textures // PBRTexture pbrTexture = null; if (withTexture) { Console.WriteLine("Download image tiles..."); TileRange tiles = _imageryService.DownloadTiles(bbox, provider, TEXTURE_TILES); string fileName = Path.Combine(outputDir, "Texture.jpg"); Console.WriteLine("Construct texture..."); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); // //======================= //======================= // Normal map Console.WriteLine("Height map..."); //float Z_FACTOR = 0.00002f; //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); var normalMap = _imageryService.GenerateNormalMap(heightMap, outputDir); pbrTexture = PBRTexture.Create(texInfo, normalMap); //hMap = hMap.CenterOnOrigin(Z_FACTOR); // //======================= } // Triangulate height map // and add base and sides _logger.LogInformation($"Triangulating height map and generating 3D mesh..."); var model = _sharpGltfService.CreateTerrainMesh(heightMap, pbrTexture); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + ".glb")); model = _sharpGltfService.CreateTerrainMesh(heightMap, GenOptions.Normals | GenOptions.BoxedBaseElevationMin); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + "_normalsBox.glb")); _logger.LogInformation($"Model exported as {Path.Combine(Directory.GetCurrentDirectory(), modelName + ".gltf")} and .glb"); _logger.LogInformation($"Done in {sw.Elapsed:g}"); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }
public void Run(DEMDataSet dataset, bool withTexture = true) { try { int TEXTURE_TILES = 4; // 4: med, 8: high //_rasterService.GenerateDirectoryMetadata(dataset, false); Stopwatch sw = Stopwatch.StartNew(); string modelName = $"MontBlanc_{dataset.Name}"; string outputDir = Directory.GetCurrentDirectory(); ImageryProvider provider = ImageryProvider.EsriWorldImagery;// new TileDebugProvider(new GeoPoint(43.5,5.5)); //// You can get your boox from https://geojson.net/ (save as WKT) //string bboxWKT = "POLYGON((5.54888 43.519525, 5.61209 43.519525, 5.61209 43.565225, 5.54888 43.565225, 5.54888 43.519525))"; //// string bboxWKT = //// "POLYGON((5.594457381483949 43.545276557046044,5.652135604140199 43.545276557046044,5.652135604140199 43.52038635099936,5.594457381483949 43.52038635099936,5.594457381483949 43.545276557046044))"; //// _logger.LogInformation($"Processing model {modelName}..."); //// //// //// _logger.LogInformation($"Getting bounding box geometry..."); //var bbox = GeometryService.GetBoundingBox(bboxWKT); // DjebelMarra var bbox = new BoundingBox(24.098067346557492, 24.42468219234563, 12.7769822830208, 13.087504129660111); // MontBlanc //var bbox = GeometryService.GetBoundingBox("POLYGON((6.618804355541963 45.9658287141746,7.052764316479463 45.9658287141746,7.052764316479463 45.72379929776474,6.618804355541963 45.72379929776474,6.618804355541963 45.9658287141746))"); //var bbox = new BoundingBox(5.5613898348431485,5.597185285307553,43.49372969433046,43.50939068558466); _logger.LogInformation($"Getting height map data..."); var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); ModelGenerationTransform transform = new ModelGenerationTransform(bbox, 3857, true, 1.5f, true); _logger.LogInformation($"Processing height map data ({heightMap.Count} coordinates)..."); heightMap = transform.TransformHeightMap(heightMap); //======================= // Textures // PBRTexture pbrTexture = null; if (withTexture) { Console.WriteLine("Download image tiles..."); TileRange tiles = _imageryService.DownloadTiles(bbox, provider, TEXTURE_TILES); string fileName = Path.Combine(outputDir, "Texture.jpg"); Console.WriteLine("Construct texture..."); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); // //======================= //======================= // Normal map Console.WriteLine("Height map..."); //float Z_FACTOR = 0.00002f; //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); var normalMap = _imageryService.GenerateNormalMap(heightMap, outputDir); pbrTexture = PBRTexture.Create(texInfo, normalMap); //hMap = hMap.CenterOnOrigin(Z_FACTOR); // //======================= } // Triangulate height map // and add base and sides _logger.LogInformation($"Triangulating height map and generating 3D mesh..."); var model = _sharpGltfService.CreateTerrainMesh(heightMap, pbrTexture); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + ".glb")); model.SaveAsWavefront(Path.Combine(Directory.GetCurrentDirectory(), modelName + ".obj")); model = _sharpGltfService.CreateTerrainMesh(heightMap, GenOptions.Normals | GenOptions.BoxedBaseElevationMin); model.SaveGLB(Path.Combine(Directory.GetCurrentDirectory(), modelName + "_normalsBox.glb")); model.SaveAsWavefront(Path.Combine(Directory.GetCurrentDirectory(), modelName + "_normalsBox.obj")); _logger.LogInformation($"Model exported as {Path.Combine(Directory.GetCurrentDirectory(), modelName + ".gltf")} and .glb"); _logger.LogInformation($"Done in {sw.Elapsed:g}"); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } }
private (ModelRoot Model, double widthMeters, double heightMeters, double averageElevation, BoundingBox projectedBbox) GenerateSampleModel(BoundingBox bbox, DEMDataSet dataset, bool withTexture = true) { try { int TEXTURE_TILES = 8; // 4: med, 8: high string outputDir = Directory.GetCurrentDirectory(); //_rasterService.GenerateDirectoryMetadata(dataset, false); ImageryProvider provider = ImageryProvider.MapBoxSatellite;// new TileDebugProvider(new GeoPoint(43.5,5.5)); _logger.LogInformation($"Getting height map data..."); var heightMap = _elevationService.GetHeightMap(ref bbox, dataset); var wgs84bbox = bbox; ModelGenerationTransform transform = new ModelGenerationTransform(bbox, 3857, true, 1.5f, true); _logger.LogInformation($"Processing height map data ({heightMap.Count} coordinates)..."); heightMap = transform.TransformHeightMap(heightMap); //======================= // Textures // PBRTexture pbrTexture = null; if (withTexture) { Console.WriteLine("Download image tiles..."); TileRange tiles = _imageryService.DownloadTiles(bbox, provider, TEXTURE_TILES); string fileName = Path.Combine(outputDir, "Texture.jpg"); Console.WriteLine("Construct texture..."); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); // //======================= //======================= // Normal map Console.WriteLine("Height map..."); //float Z_FACTOR = 0.00002f; //hMap = hMap.CenterOnOrigin().ZScale(Z_FACTOR); //var normalMap = _imageryService.GenerateNormalMap(heightMap, outputDir); pbrTexture = PBRTexture.Create(texInfo, null); //hMap = hMap.CenterOnOrigin(Z_FACTOR); // //======================= } // Triangulate height map // and add base and sides _logger.LogInformation($"Triangulating height map and generating 3D mesh..."); var model = _sharpGltfService.CreateTerrainMesh(heightMap, pbrTexture); var width = new GeoPoint(wgs84bbox.Center[1], bbox.Center[0] - bbox.Width / 2f).DistanceTo(new GeoPoint(wgs84bbox.Center[1], bbox.Center[0] + bbox.Width / 2f)); var height = new GeoPoint(bbox.Center[1] - bbox.Height / 2f, wgs84bbox.Center[0]).DistanceTo(new GeoPoint(bbox.Center[1] + bbox.Height, wgs84bbox.Center[0])); return(model, width, height, wgs84bbox.Center[2], heightMap.BoundingBox); } catch (Exception ex) { _logger.LogError(ex, ex.Message); return(null, 0, 0, 0, null); } }
private Location3DModelResponse Generate3DLocationModel(Location3DModelRequest request, Location3DModelSettings settings) { Location3DModelResponse response = new Location3DModelResponse(); try { bool imageryFailed = false; using (TimeSpanBlock timer = new TimeSpanBlock($"3D model {request.Id}", _logger)) { BoundingBox bbox = GetBoundingBoxAroundLocation(request.Latitude, request.Longitude, settings.SideSizeKm); HeightMap hMap = _elevationService.GetHeightMap(ref bbox, settings.Dataset); var transform = new ModelGenerationTransform(bbox, Reprojection.SRID_PROJECTED_MERCATOR, centerOnOrigin: true, settings.ZScale, centerOnZOrigin: true); response.Attributions.AddRange(settings.Attributions); // will be added to the model response.Attributions.Add(settings.Dataset.Attribution); // will be added to the model PBRTexture pbrTexture = null; if (settings.ImageryProvider != null) { response.Attributions.Add(settings.ImageryProvider.Attribution); // will be added to the model // Imagery TileRange tiles = _imageryService.ComputeBoundingBoxTileRange(bbox, settings.ImageryProvider, settings.MinTilesPerImage); Debug.Assert(tiles.Count < 400); tiles = _imageryService.DownloadTiles(tiles, settings.ImageryProvider); string fileName = Path.Combine(settings.OutputDirectory, $"{request.Id}_Texture.jpg"); TextureInfo texInfo = _imageryService.ConstructTexture(tiles, bbox, fileName, TextureImageFormat.image_jpeg); transform.BoundingBox = bbox; hMap = transform.TransformHeightMap(hMap); //var normalMap = _imageryService.GenerateNormalMap(hMap, settings.OutputDirectory, $"{request.Id}_normalmap.png"); pbrTexture = PBRTexture.Create(texInfo); } // Center on origin //hMap = hMap.CenterOnOrigin(out Matrix4x4 transform).BakeCoordinates(); //response.Origin = new GeoPoint(request.Latitude, request.Longitude).ReprojectTo(Reprojection.SRID_GEODETIC, Reprojection.SRID_PROJECTED_MERCATOR); ModelRoot model = _gltfService.CreateNewModel(); //======================= // Buildings if (settings.OsmBuildings) { model = _sampleOsmProcessor.Run(model, OsmLayer.Buildings, bbox, transform, computeElevations: true, settings.Dataset, settings.DownloadMissingFiles); } if (settings.GenerateTIN) { model = AddTINMesh(model, hMap, 2d, _gltfService, pbrTexture, Reprojection.SRID_PROJECTED_MERCATOR); } else { model = _gltfService.AddTerrainMesh(model, hMap, pbrTexture); } model.Asset.Generator = "DEM Net Elevation API with SharpGLTF"; //model.TryUseExtrasAsList(true).AddRange(response.Attributions); model.SaveGLB(Path.Combine(settings.OutputDirectory, string.Concat(imageryFailed ? "imageryFailed_" : "", settings.ModelFileNameGenerator(settings, request)))); // cleanup //if (pbrTexture != null) //{ // if (pbrTexture.NormalTexture != null) File.Delete(pbrTexture.NormalTexture.FilePath); // File.Delete(pbrTexture.BaseColorTexture.FilePath); //} response.Elapsed = timer.Elapsed; response.NumTiles = pbrTexture.BaseColorTexture.TileCount; } } catch (Exception) { throw; } return(response); }