Fix: snapping now also works if the building it snaps to is a multipolygon/relation

This commit is contained in:
Pieter Vander Vennet 2025-03-07 00:32:20 +01:00
parent d224f1fd39
commit fc940c8a7e
4 changed files with 123 additions and 65 deletions

View file

@ -10,11 +10,12 @@ import {
MultiPolygon,
Point,
Polygon,
Position,
Position
} from "geojson"
import { Tiles } from "../Models/TileRange"
import { Utils } from "../Utils"
;("use strict")
("use strict")
export class GeoOperations {
private static readonly _earthRadius = 6378137
@ -28,7 +29,7 @@ export class GeoOperations {
"behind",
"sharp_left",
"left",
"slight_left",
"slight_left"
] as const
private static reverseBearing = {
N: 0,
@ -46,7 +47,7 @@ export class GeoOperations {
W: 270,
WNW: 292.5,
NW: 315,
NNW: 337.5,
NNW: 337.5
}
/**
@ -308,7 +309,7 @@ export class GeoOperations {
bufferSizeInMeter: number
): Feature<Polygon | MultiPolygon> | FeatureCollection<Polygon | MultiPolygon> {
return turf.buffer(feature, bufferSizeInMeter / 1000, {
units: "kilometers",
units: "kilometers"
})
}
@ -324,9 +325,9 @@ export class GeoOperations {
[lon0, lat],
[lon0, lat0],
[lon, lat0],
[lon, lat],
],
},
[lon, lat]
]
}
}
}
@ -356,40 +357,47 @@ export class GeoOperations {
* Mostly used as helper for 'nearestPoint'
* @param way
*/
public static forceLineString(way: Feature<LineString | Polygon>): Feature<LineString>
public static forceLineString(way: Feature<LineString | Polygon>): Feature<LineString>[]
public static forceLineString(
way: Feature<MultiLineString | MultiPolygon>
): Feature<MultiLineString>
): Feature<MultiLineString>[]
public static forceLineString(
way: Feature<LineString | MultiLineString | Polygon | MultiPolygon>
): Feature<LineString | MultiLineString> {
): Feature<LineString | MultiLineString>[] {
if (way.geometry.type === "Polygon") {
return <Feature<LineString>>{
type: "Feature",
geometry: {
type: "LineString",
coordinates: way.geometry.coordinates[0],
},
properties: way.properties,
}
const poly: Feature<Polygon> = <Feature<Polygon>>way
return poly.geometry.coordinates.map((linestringCoors, i) => {
return <Feature<LineString>>{
type: "Feature",
geometry: {
type: "LineString",
coordinates: linestringCoors
},
properties: way.properties
}
})
}
if (way.geometry.type === "MultiPolygon") {
return <Feature<MultiLineString>>{
type: "Feature",
geometry: {
type: "MultiLineString",
coordinates: way.geometry.coordinates[0],
},
properties: way.properties,
}
const mpoly: Feature<MultiPolygon> = <Feature<MultiPolygon>>way
return [].concat(...mpoly.geometry.coordinates.map(linestrings =>
[].concat(...linestrings.map(linestring =>
<Feature<LineString>>{
type: "Feature",
geometry: {
type: "LineString",
coordinates: linestring
},
properties: way.properties
}))))
}
if (way.geometry.type === "LineString") {
return <Feature<LineString>>way
return [<Feature<LineString>>way]
}
if (way.geometry.type === "MultiLineString") {
return <Feature<MultiLineString>>way
return [<Feature<MultiLineString>>way]
}
throw "Invalid geometry to create a way from this"
}
@ -466,7 +474,7 @@ export class GeoOperations {
const lon = lonLat[0]
const lat = lonLat[1]
const x = (180 * lon) / GeoOperations._originShift
let y = (180 * lat) / GeoOperations._originShift
let y = (180 * lat) / GeoOperations._originShiftcons
y = (180 / Math.PI) * (2 * Math.atan(Math.exp((y * Math.PI) / 180)) - Math.PI / 2)
return [x, y]
}
@ -560,7 +568,7 @@ export class GeoOperations {
}
const properties = {
...f.properties,
id,
id
}
intersectionPart.properties = properties
newFeatures.push(intersectionPart)
@ -592,8 +600,8 @@ export class GeoOperations {
properties: {},
geometry: {
type: "Point",
coordinates: p,
},
coordinates: p
}
}
)
}
@ -609,7 +617,7 @@ export class GeoOperations {
trackPoints.push(trkpt)
}
const header =
'<gpx version="1.1" creator="mapcomplete.org" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'
"<gpx version=\"1.1\" creator=\"mapcomplete.org\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">"
return (
header +
"\n<name>" +
@ -648,7 +656,7 @@ export class GeoOperations {
trackPoints.push(trkpt)
}
const header =
'<gpx version="1.1" creator="mapcomplete.org" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'
"<gpx version=\"1.1\" creator=\"mapcomplete.org\" xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">"
return (
header +
"\n<name>" +
@ -674,7 +682,7 @@ export class GeoOperations {
const copy = {
...feature,
geometry: { ...feature.geometry },
geometry: { ...feature.geometry }
}
let coordinates: [number, number][]
if (feature.geometry.type === "LineString") {
@ -732,8 +740,8 @@ export class GeoOperations {
type: "Feature",
geometry: {
type: "LineString",
coordinates: [a, b],
},
coordinates: [a, b]
}
},
distanceMeter,
{ units: "meters" }
@ -780,8 +788,8 @@ export class GeoOperations {
type: "Feature",
geometry: {
type: "Polygon",
coordinates,
},
coordinates
}
}
)
return !polygons.some((polygon) => !booleanWithin(polygon, possiblyEnclosingFeature))
@ -860,8 +868,8 @@ export class GeoOperations {
type: "Feature",
properties: { ...toSplit.properties },
geometry: boundary.geometry,
bbox: boundary.bbox,
},
bbox: boundary.bbox
}
]
}
return []
@ -959,8 +967,8 @@ export class GeoOperations {
properties: p.properties,
geometry: {
type: "LineString",
coordinates: p.geometry.coordinates[0],
},
coordinates: p.geometry.coordinates[0]
}
}
}
@ -988,7 +996,7 @@ export class GeoOperations {
console.debug("Splitting way", feature.properties.id)
result.push(<Feature>{
...feature,
geometry: { ...feature.geometry, coordinates: coors.slice(i + 1) },
geometry: { ...feature.geometry, coordinates: coors.slice(i + 1) }
})
coors = coors.slice(0, i + 1)
break
@ -997,7 +1005,7 @@ export class GeoOperations {
}
result.push(<Feature>{
...feature,
geometry: { ...feature.geometry, coordinates: coors },
geometry: { ...feature.geometry, coordinates: coors }
})
}
}
@ -1171,8 +1179,8 @@ export class GeoOperations {
properties: multiLineStringFeature.properties,
geometry: {
type: "LineString",
coordinates: coors[0],
},
coordinates: coors[0]
}
}
}
return {
@ -1180,8 +1188,8 @@ export class GeoOperations {
properties: multiLineStringFeature.properties,
geometry: {
type: "MultiLineString",
coordinates: coors,
},
coordinates: coors
}
}
}
@ -1334,7 +1342,7 @@ export class GeoOperations {
const intersection = turf.intersect(
turf.featureCollection([
turf.truncate(feature),
turf.truncate(otherFeature),
turf.truncate(otherFeature)
])
)
if (intersection == null) {