Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cutlines support #240

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GameRealisticMap/BuildersCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GameRealisticMap.ManMade;
using GameRealisticMap.ManMade.Airports;
using GameRealisticMap.ManMade.Buildings;
using GameRealisticMap.ManMade.Cutlines;
using GameRealisticMap.ManMade.DefaultUrbanAreas;
using GameRealisticMap.ManMade.Farmlands;
using GameRealisticMap.ManMade.Fences;
Expand Down Expand Up @@ -81,6 +82,7 @@ public BuildersCatalog(IProgressSystem progress, IBuildersConfig config, ISource
Register(new AirportBuilder(progress));
Register(new AerowaysBuilder(progress));
Register(new AsphaltBuilder(progress));
Register(new CutlinesBuilder(progress));
}

public void Register<TData>(IDataBuilder<TData> builder)
Expand Down
64 changes: 64 additions & 0 deletions GameRealisticMap/ManMade/Cutlines/CutlinesBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Globalization;
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade.Roads.Libraries;
using GameRealisticMap.Reporting;
using OsmSharp.Tags;

namespace GameRealisticMap.ManMade.Cutlines
{
internal class CutlinesBuilder : IDataBuilder<CutlinesData>
{
private readonly IProgressSystem progress;

public CutlinesBuilder(IProgressSystem progress)
{
this.progress = progress;
}

public CutlinesData Build(IBuildContext context)
{
var polygons = new List<TerrainPolygon>();

foreach(var way in context.OsmSource.Ways
.Where(w => w.Tags != null && w.Tags.GetValue("man_made") == "cutline")
.ProgressStep(progress, "Interpret"))
{
polygons.AddRange(
context.OsmSource.Interpret(way)
.SelectMany(w => TerrainPath.FromGeometry(w, context.Area.LatLngToTerrainPoint))
.Where(p => p.EnveloppeIntersects(context.Area.TerrainBounds))
.SelectMany(p => p.ClippedBy(context.Area.TerrainBounds))
.SelectMany(p => p.ToTerrainPolygon(GetWidth(way.Tags))));
}

return new CutlinesData(polygons);
}

private float GetWidth(TagsCollectionBase tags)
{
if(tags.TryGetValue("width", out var widthStr)
&& !string.IsNullOrEmpty(widthStr)
&& float.TryParse(widthStr, NumberStyles.Number, CultureInfo.InvariantCulture, out var widthValue))
{
return widthValue;
}
switch(tags.GetValue("cutline"))
{
case "section":
case "border":
case "loggingmachine":
case "logway":
case "pipeline":
return 5;

case "firebreak":
case "power_line":
return 10;

case "piste":
return 15;
}
return 10;
}
}
}
33 changes: 33 additions & 0 deletions GameRealisticMap/ManMade/Cutlines/CutlinesData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade.Railways;
using GameRealisticMap.Nature;
using GeoJSON.Text.Feature;
using GeoJSON.Text.Geometry;

namespace GameRealisticMap.ManMade.Cutlines
{
public class CutlinesData : IGeoJsonData//, INonDefaultArea
{
[JsonConstructor]
public CutlinesData(List<TerrainPolygon> polygons)
{
Polygons = polygons;
}

public List<TerrainPolygon> Polygons { get; }

public IEnumerable<Feature> ToGeoJson(Func<TerrainPoint, IPosition> project)
{
var properties = new Dictionary<string, object>() {
{"type", "cutline" }
};
return Polygons.Select(r => new Feature(r.ToGeoJson(project), properties));
}
}
}
6 changes: 5 additions & 1 deletion GameRealisticMap/Nature/Forests/ForestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade.Cutlines;
using GameRealisticMap.Reporting;
using OsmSharp.Tags;

Expand All @@ -24,7 +25,10 @@ protected override bool IsTargeted(TagsCollectionBase tags)

protected override IEnumerable<TerrainPolygon> GetPriority(IBuildContext context)
{
return base.GetPriority(context);
var cutlines = context.GetData<CutlinesData>();

return base.GetPriority(context)
.Concat(cutlines.Polygons);
}
}
}
6 changes: 5 additions & 1 deletion GameRealisticMap/Nature/Forests/ForestRadialBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade.Cutlines;
using GameRealisticMap.Reporting;

namespace GameRealisticMap.Nature.Forests
Expand All @@ -12,7 +13,10 @@ public ForestRadialBuilder(IProgressSystem progress) : base(progress, ForestRadi

protected override IEnumerable<TerrainPolygon> GetPriority(IBuildContext context)
{
return base.GetPriority(context);
var cutlines = context.GetData<CutlinesData>();

return base.GetPriority(context)
.Concat(cutlines.Polygons);
}

protected override ForestRadialData CreateWrapper(List<TerrainPolygon> polygons)
Expand Down
Loading