Do not show out-of-range features on speelplekken layer, fix handling of mutlipolygons in 'inside', better tests

This commit is contained in:
Pieter Vander Vennet 2021-05-14 02:25:30 +02:00
parent 117b0bddb1
commit 6f457a6f0d
26 changed files with 1284 additions and 770 deletions

View file

@ -10,9 +10,9 @@ import {TagsFilter} from "../Tags/TagsFilter";
import SimpleMetaTagger from "../SimpleMetaTagger";
export default class UpdateFromOverpass implements FeatureSource {
export default class OverpassFeatureSource implements FeatureSource {
public readonly name = "UpdateFromOverpass"
public readonly name = "OverpassFeatureSource"
/**
* The last loaded features of the geojson

View file

@ -84,7 +84,6 @@ export class ElementStorage {
}
}
if (somethingChanged) {
console.trace(`Merging multiple instances of ${elementId}: ` + debug_msg.join(", ")+" newProperties: ", newProperties)
es.ping();
}
return es;

View file

@ -38,7 +38,7 @@ Some advanced functions are available on <b>feat</b> as well:
`
private static readonly OverlapFunc = new ExtraFunction(
"overlapWith",
"Gives a list of features from the specified layer which this feature overlaps with, the amount of overlap in m². The returned value is <b>{ feat: GeoJSONFeature, overlap: number}</b>",
"Gives a list of features from the specified layer which this feature (partly) overlaps with. If the current feature is a point, all features that embed the point are given. The returned value is <code>{ feat: GeoJSONFeature, overlap: number}[]</code> where <code>overlap</code> is the overlapping surface are (in m²) for areas, the overlapping length (in meter) if the current feature is a line or <code>undefined</code> if the current feature is a point",
["...layerIds - one or more layer ids of the layer from which every feature is checked for overlap)"],
(params, feat) => {
return (...layerIds: string[]) => {

View file

@ -6,16 +6,15 @@ export class GeoOperations {
return turf.area(feature);
}
static centerpoint(feature: any)
{
const newFeature= turf.center(feature);
static centerpoint(feature: any) {
const newFeature = turf.center(feature);
newFeature.properties = feature.properties;
newFeature.id = feature.id;
return newFeature;
}
static centerpointCoordinates(feature: any): [number, number]{
static centerpointCoordinates(feature: any): [number, number] {
// @ts-ignore
return turf.center(feature).geometry.coordinates;
}
@ -25,36 +24,100 @@ export class GeoOperations {
* @param lonlat0
* @param lonlat1
*/
static distanceBetween(lonlat0: [number,number], lonlat1:[number, number]){
static distanceBetween(lonlat0: [number, number], lonlat1: [number, number]) {
return turf.distance(lonlat0, lonlat1)
}
/**
* Calculates the overlap of 'feature' with every other specified feature.
* The features with which 'feature' overlaps, are returned together with their overlap area in m²
*
*
* If 'feature' is a LineString, the features in which this feature is (partly) embedded is returned, the overlap length in meter is given
*
* If 'feature' is a point, it will return every feature the point is embedded in. Overlap will be undefined
*/
static calculateOverlap(feature: any,
otherFeatures: any[]): { feat: any, overlap: number }[] {
static calculateOverlap(feature: any, otherFeatures: any[]): { feat: any, overlap: number }[] {
const featureBBox = BBox.get(feature);
const result : { feat: any, overlap: number }[] = [];
const result: { feat: any, overlap: number }[] = [];
if (feature.geometry.type === "Point") {
const coor = feature.geometry.coordinates;
for (const otherFeature of otherFeatures) {
if (otherFeature.geometry === undefined) {
console.error("No geometry for feature ", feature)
throw "List of other features contains a feature without geometry an undefined"
}
let otherFeatureBBox = BBox.get(otherFeature);
if (!featureBBox.overlapsWith(otherFeatureBBox)) {
if (!featureBBox.overlapsWith(otherFeatureBBox)) {
continue;
}
if (this.inside(coor, otherFeatures)) {
result.push({ feat: otherFeatures, overlap: undefined })
if (this.inside(coor, otherFeature)) {
result.push({feat: otherFeature, overlap: undefined})
}
}
return result;
}
if (feature.geometry.type === "LineString") {
for (const otherFeature of otherFeatures) {
const otherFeatureBBox = BBox.get(otherFeature);
const overlaps = featureBBox.overlapsWith(otherFeatureBBox)
if (!overlaps) {
continue;
}
// Calculate the length of the intersection
try {
let intersectionPoints = turf.lineIntersect(feature, otherFeature);
if (intersectionPoints.features.length == 0) {
// No intersections.
// If one point is inside of the polygon, all points are
const coors = feature.geometry.coordinates;
const startCoor = coors[0]
if (this.inside(startCoor, otherFeature)) {
result.push({feat: otherFeature, overlap: this.lengthInMeters(feature)})
}
continue;
}
let intersectionPointsArray = intersectionPoints.features.map(d => {
return d.geometry.coordinates
});
if (intersectionPointsArray.length == 1) {
// We need to add the start- or endpoint of the current feature, depending on which one is embedded
const coors = feature.geometry.coordinates;
const startCoor = coors[0]
if (this.inside(startCoor, otherFeature)) {
// The startpoint is embedded
intersectionPointsArray.push(startCoor)
} else {
intersectionPointsArray.push(coors[coors.length - 1])
}
}
let intersection = turf.lineSlice(turf.point(intersectionPointsArray[0]), turf.point(intersectionPointsArray[1]), feature);
if (intersection == null) {
continue;
}
const intersectionSize = turf.length(intersection); // in km
result.push({feat: otherFeature, overlap: intersectionSize * 1000})
} catch (exception) {
console.warn("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception);
}
}
return result;
}
if (feature.geometry.type === "Polygon" || feature.geometry.type === "MultiPolygon") {
for (const otherFeature of otherFeatures) {
@ -74,7 +137,7 @@ export class GeoOperations {
const intersectionSize = turf.area(intersection); // in m²
result.push({feat: otherFeature, overlap: intersectionSize})
} catch (exception) {
console.log("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception);
console.warn("EXCEPTION CAUGHT WHILE INTERSECTING: ", exception);
}
}
@ -83,7 +146,7 @@ export class GeoOperations {
console.error("Could not correctly calculate the overlap of ", feature, ": unsupported type")
return result;
}
public static inside(pointCoordinate, feature): boolean {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
@ -92,6 +155,32 @@ export class GeoOperations {
return false;
}
if (feature.geometry.type === "MultiPolygon") {
const coordinates = feature.geometry.coordinates[0];
const outerPolygon = coordinates[0];
const inside = GeoOperations.inside(pointCoordinate, {
geometry: {
type: 'Polygon',
coordinates: [outerPolygon]
}
})
if (!inside) {
return false;
}
for (let i = 1; i < coordinates.length; i++) {
const inHole = GeoOperations.inside(pointCoordinate, {
geometry: {
type: 'Polygon',
coordinates: [coordinates[i]]
}
})
if (inHole) {
return false;
}
}
return true;
}
const x: number = pointCoordinate[0];
const y: number = pointCoordinate[1];
@ -125,7 +214,7 @@ export class GeoOperations {
}
class BBox{
class BBox {
readonly maxLat: number;
readonly maxLon: number;
@ -148,29 +237,6 @@ class BBox{
this.check();
}
private check() {
if (isNaN(this.maxLon) || isNaN(this.maxLat) || isNaN(this.minLon) || isNaN(this.minLat)) {
console.log(this);
throw "BBOX has NAN";
}
}
public overlapsWith(other: BBox) {
this.check();
other.check();
if (this.maxLon < other.minLon) {
return false;
}
if (this.maxLat < other.minLat) {
return false;
}
if (this.minLon > other.maxLon) {
return false;
}
return this.minLat <= other.maxLat;
}
static get(feature) {
if (feature.bbox?.overlapsWith === undefined) {
@ -195,4 +261,27 @@ class BBox{
return feature.bbox;
}
public overlapsWith(other: BBox) {
this.check();
other.check();
if (this.maxLon < other.minLon) {
return false;
}
if (this.maxLat < other.minLat) {
return false;
}
if (this.minLon > other.maxLon) {
return false;
}
return this.minLat <= other.maxLat;
}
private check() {
if (isNaN(this.maxLon) || isNaN(this.maxLat) || isNaN(this.minLon) || isNaN(this.minLat)) {
console.log(this);
throw "BBOX has NAN";
}
}
}

View file

@ -26,7 +26,6 @@ export default class MetaTagging {
layers: LayerConfig[],
includeDates = true) {
console.debug("Adding meta tags to all features")
for (const metatag of SimpleMetaTagger.metatags) {
if (metatag.includesDates && !includeDates) {
// We do not add dated entries
@ -95,9 +94,17 @@ export default class MetaTagging {
const f = (featuresPerLayer, feature: any) => {
try {
feature.properties[key] =func(feature);
let result = func(feature);
if(result === undefined || result === ""){
return;
}
if(typeof result !== "string"){
// Make sure it is a string!
result = "" + result;
}
feature.properties[key] = result;
} catch (e) {
console.error("Could not calculate a metatag defined by " + code + " due to " + e + ". This is code defined in the theme. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features")
console.error("Could not calculate a metatag defined by " + code + " due to " + e + ". This is code defined in the theme. Are you the theme creator? Doublecheck your code. Note that the metatags might not be stable on new features", e)
}
}

View file

@ -49,7 +49,7 @@ export class RegexTag extends TagsFilter {
continue;
}
if (RegexTag.doesMatch(key, this.key)) {
const value = tags[key]
const value = tags[key] ?? "";
return RegexTag.doesMatch(value, this.value) != this.invert;
}
}