Skip to content

Commit

Permalink
Require GeoJsonFeatures to have geometry
Browse files Browse the repository at this point in the history
Despite spec allowing non-located features, we need a location, so will reject features that lack a geometry
  • Loading branch information
2kai2kai2 committed Dec 5, 2023
1 parent d5fd4e2 commit 792a98d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,18 @@ class GDASpatialDataResultEntity: Object {
}

// Set geolocation information
if let geometry = feature.geometry {
if case .point(let point) = geometry {
latitude = point.latitude
longitude = point.longitude
} else if let json_data = try? JSONEncoder().encode(geometry) {
coordinatesJson = String(data: json_data, encoding: .utf8)
}

let centroid = geometry.centroid
centroidLatitude = centroid.latitude
centroidLongitude = centroid.longitude
if case .point(let point) = feature.geometry {
latitude = point.latitude
longitude = point.longitude
} else if let json_data = try? JSONEncoder().encode(feature.geometry) {
coordinatesJson = String(data: json_data, encoding: .utf8)
_geometry = feature.geometry;
}

let centroid = feature.geometry.centroid
centroidLatitude = centroid.latitude
centroidLongitude = centroid.longitude

// Road specific metadata

roundabout = feature.isRoundabout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ final class GeoJsonFeature: Decodable {
var superCategory: SuperCategory = .undefined

/// Geometry of this feature
/// While Features are required to have a `geometry` member, it may be `null` for unlocated features, represented here as `nil`
var geometry: GeoJsonGeometry?
///
/// GeoJSON spec allows null geometries, but we don't (we throw a parse failure if missing)
var geometry: GeoJsonGeometry

var isCrossing = false

Expand Down Expand Up @@ -143,12 +144,12 @@ final class GeoJsonFeature: Decodable {
isRoundabout = true
}

// TODO: it is unclear if geometry not existing should cause the feature to be rejected
geometry = try? container.decode(GeoJsonGeometry.self, forKey: .geometry)
// GeoJSON spec permits null geometries, but we don't
geometry = try container.decode(GeoJsonGeometry.self, forKey: .geometry)

// Fix geometries for crossings with LineString geometries
if isCrossing, case .lineString = geometry,
let median = geometry?.getLineMedian() {
let median = geometry.getLineMedian() {
geometry = GeoJsonGeometry(point: median)
}

Expand Down Expand Up @@ -272,14 +273,10 @@ final class GeoJsonFeature: Decodable {
let startCopy = GeoJsonFeature(copyFrom: self)
let endCopy = GeoJsonFeature(copyFrom: self)

guard let first = geometry?.first, let last = geometry?.last else {
return nil
}

startCopy.geometry = .point(coordinates: first)
startCopy.geometry = .point(coordinates: geometry.first)
startCopy.superCategory = SuperCategory.mobility

endCopy.geometry = .point(coordinates: last)
endCopy.geometry = .point(coordinates: geometry.last)
endCopy.superCategory = SuperCategory.mobility

return (startCopy, endCopy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class GeoJsonFeatureTest: XCTestCase {
XCTAssertEqual(rpi_feature.type, "amenity")
XCTAssertEqual(rpi_feature.value, "university")
XCTAssertEqual(rpi_feature.osmIds, ["ft-100000000008670722"])
XCTAssertEqual(rpi_feature.geometry?.rawValue, "MultiPolygon")
XCTAssertEqual(rpi_feature.geometry.rawValue, "MultiPolygon")
//XCTAssertEqual(rpi_feature.superCategory, .undefined)

XCTAssertEqual(rpi_feature.properties, [
Expand Down Expand Up @@ -105,7 +105,7 @@ final class GeoJsonFeatureTest: XCTestCase {
XCTAssertEqual(sage_feature.type, "highway")
XCTAssertEqual(sage_feature.value, "tertiary")
XCTAssertEqual(sage_feature.osmIds, ["ft-669453514"])
XCTAssertEqual(sage_feature.geometry?.rawValue, "LineString")
XCTAssertEqual(sage_feature.geometry.rawValue, "LineString")

XCTAssertEqual(sage_feature.properties, [
"highway": "tertiary",
Expand Down

0 comments on commit 792a98d

Please sign in to comment.