Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
53 lines
2.5 KiB
Python
53 lines
2.5 KiB
Python
"""Descriptive geometry metrics; these are not labeled accuracy measurements."""
|
|
from collections import Counter
|
|
|
|
import numpy as np
|
|
import shapely
|
|
from shapely.geometry import shape
|
|
|
|
|
|
def inspect(document):
|
|
rows = [(f, shape(f['geometry'])) for f in document['features']]
|
|
classes = sorted({f['properties']['class'] for f, _ in rows})
|
|
result = dict(invalid_ids=[], nonfinite_ids=[], classes={},
|
|
marking_subtypes=dict(Counter(f['properties'].get('subtype', 'unknown')
|
|
for f, _ in rows if f['properties']['class'] == 'road_marking')),
|
|
ground_truth_used=False)
|
|
for f, g in rows:
|
|
if g.is_empty or not g.is_valid:
|
|
result['invalid_ids'].append(f['id'])
|
|
coordinates = shapely.get_coordinates(g, include_z=g.has_z)
|
|
if not np.isfinite(coordinates).all():
|
|
result['nonfinite_ids'].append(f['id'])
|
|
for cls in classes:
|
|
geometries = [shapely.force_2d(g) for f, g in rows
|
|
if f['properties']['class'] == cls and g.is_valid]
|
|
polygons = [g for g in geometries if g.geom_type in ('Polygon', 'MultiPolygon')]
|
|
union = shapely.union_all(polygons)
|
|
result['classes'][cls] = dict(features=len(geometries),
|
|
polygon_area_m2=float(sum(g.area for g in polygons)),
|
|
union_area_m2=float(union.area),
|
|
overlap_excess_m2=float(max(0, sum(g.area for g in polygons) - union.area)),
|
|
line_length_m=float(sum(g.length for g in geometries
|
|
if g.geom_type in ('LineString', 'MultiLineString'))),
|
|
holes=sum(len(p.interiors) for g in polygons
|
|
for p in (list(g.geoms) if g.geom_type == 'MultiPolygon' else [g])))
|
|
return result
|
|
|
|
|
|
def difference(reference, candidate):
|
|
classes = {f['properties']['class'] for d in [reference, candidate] for f in d['features']}
|
|
result = {}
|
|
for cls in sorted(classes):
|
|
unions = []
|
|
for doc in [reference, candidate]:
|
|
geometries = [shapely.force_2d(shape(f['geometry'])) for f in doc['features']
|
|
if f['properties']['class'] == cls]
|
|
unions.append(shapely.union_all([g for g in geometries if g.is_valid]))
|
|
a, b = unions
|
|
delta = a.symmetric_difference(b)
|
|
result[cls] = dict(symmetric_difference_area_m2=float(delta.area),
|
|
symmetric_difference_length_m=float(delta.length),
|
|
union_equal=bool(a.equals(b)))
|
|
return result
|