Permalink
Browse files

Clean up invalid geoms by simplification

  • Loading branch information...
nikolai-b committed Nov 11, 2017
1 parent b077baf commit dd9d0e5afd049ff2da779cc55928a3db1cb73cc9
Showing with 33 additions and 8 deletions.
  1. +7 −1 app/api/route/issue_api.rb
  2. +26 −7 spec/api/route/issue_api_spec.rb
@@ -60,7 +60,13 @@ def post_or_get_issue
scope = scope.order_by_size
end
scope = scope.intersects_not_covered(params[:bbox].to_geometry) if params[:bbox].present?
scope = scope.intersects_not_covered(params[:geo_collection].map(&:geometry).inject(&:union)) if params[:geo_collection]
if params[:geo_collection]
geo_collection = params[:geo_collection].map do |itm|
itm_geo = itm.geometry
itm_geo.valid? ? itm_geo : itm_geo.simplify(0.5)
end.inject(&:union)
scope = scope.intersects_not_covered(geo_collection)
end
scope = scope.where_tag_names_in(params[:tags]) if params[:tags]
scope = scope.where_tag_names_not_in(params[:excluding_tags]) if params[:excluding_tags]
scope = scope.before_date(params[:end_date]) if params[:end_date]
@@ -27,15 +27,34 @@
before do
create :issue
geo_features = locables.map { |loc| RGeo::GeoJSON::Feature.new(loc.location) }
geo_collection = RGeo::GeoJSON.encode(RGeo::GeoJSON::FeatureCollection.new(geo_features))
get "#{host}/api/issues", geo_collection: geo_collection.to_json
get "#{host}/api/issues", geo_collection: geo_collection
end
it 'returns issue' do
expect(geojson_response.size).to eq(1)
expect(geojson_response[0]['id']).to eq(issue.id)
expect(last_response.status).to eq(200)
context "with geometries" do
let(:geo_features) { locables.map { |loc| RGeo::GeoJSON::Feature.new(loc.location) } }
let(:geo_collection) { RGeo::GeoJSON.encode(RGeo::GeoJSON::FeatureCollection.new(geo_features)).to_json }
it 'returns issue' do
expect(geojson_response.size).to eq(1)
expect(geojson_response[0]['id']).to eq(issue.id)
expect(last_response.status).to eq(200)
end
end
context "with an invalid geometry" do # e.g. the Polygon has self-interestion
let(:geo_collection) do <<-GEO
{"type":"FeatureCollection",
"features":[
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-0.15071868896484375,51.652323870435474],[-0.1535377502441667,51.647943918259216],[-0.16642570495605466,51.63016558661912],[-0.16761398315432496,51.611607930855016],[-0.1970672607421875,51.5922157026931],[-0.20333290100097656,51.59088261613596],[-0.20685195922851562,51.59056266954099],[-0.2187824249267578,51.579576469589995],[-0.23989677429199216,51.57872306133139],[-0.23903846740722656,51.585389886893026],[-0.23569107055664062,51.5921090572082],[-0.2362060546875,51.605651030611924],[-0.2077960968017578,51.61172761133676],[-0.20161628723144528,51.60895593856035],[-0.1929473876953125,51.614445818337245],[-0.18342018127441403,51.630911474166794],[-0.20977020263671875,51.64177872993458],[-0.2006721496582031,51.64721138136228],[-0.20376205444335935,51.64923514389964],[-0.2410125732421875,51.679580741628975],[-0.2767181396484375,51.71639405046408],[-0.35430908203125,51.71724487103152],[-0.37078857421875,51.736384102036176],[-0.358428955078125,51.754240074033525],[-0.317230224609375,51.767839887322154],[-0.33096313476562494,51.81837825714121],[-0.248565673828125,51.87733877579231],[-0.218353271484375,51.84595933666335],[-0.2032470703125,51.791629704426924],[-0.19775390625,51.82050047836314],[-0.208740234375,51.85783521205157],[-0.20050048828125,51.798424491278745],[-0.22659301757812497,51.90869633027845],[-0.208740234375,51.84765608216451],[-0.23345947265625,51.90446010157102],[-0.190887451171875,51.831534417403034],[-0.22796630859374997,51.90446010157102],[-0.19500732421874997,51.876490970614775],[-0.17990112304687497,51.89090148463458],[-0.199127197265625,51.89344403698398],[-0.166168212890625,51.84002022371389],[-0.22521972656249997,51.851049381288874],[-0.1593017578125,51.779736363786355],[-0.199127197265625,51.86292391360244],[-0.1682281494140625,51.892596535517995],[-0.1702880859375,51.85529064543267],[-0.168914794921875,51.82813964710642],[-0.22521972656249997,51.82389582440221],[-0.1922607421875,51.82389582440221],[-0.11398315429687499,51.76996448812036],[-0.10711669921875,51.66627377119407],[-0.15071868896484375,51.652323870435474]]]},"properties":{"thumbnail":null,"anchor":null}},
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-0.2244484235794024,51.76748],[-0.22530623834420271,51.76911379685293],[-0.22755202655450155,51.770123509271215],[-0.23032797344549844,51.770123509271215],[-0.23257376165579727,51.76911379685293],[-0.2334315764205976,51.76748],[-0.23257376165579727,51.76584614401134],[-0.23032797344549844,51.76483633590937],[-0.22755202655450155,51.76483633590937],[-0.22530623834420271,51.76584614401134],[-0.2244484235794024,51.76748]]]},"properties":{"thumbnail":null,"anchor":null}}
]
}
GEO
end
it 'returns issue' do
expect(last_response.status).to eq(200)
end
end
end

0 comments on commit dd9d0e5

Please sign in to comment.