-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task to backfill location polygons centroid
The task will calculate and populate the centroid of the location polygons with an area but not stored centroid info.
- Loading branch information
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace :location_polygons do | ||
desc "Computes and set centroid on location polygons containing an area but no centroid" | ||
task backfill_centroid: :environment do | ||
LocationPolygon.where.not(area: nil).where(centroid: nil).update_all("centroid = ST_Centroid(area)") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "location_polygons:backfill_centroid" do | ||
let(:area) { "POLYGON((0 0, 1 1, 0 1, 0 0))" } | ||
let(:centroid) { "POINT(0.33331264776372055 0.6666929898148579)" } | ||
|
||
it "computes and sets the centroid on location polygons containing an area but no centroid" do | ||
location_polygon = create(:location_polygon, area: area, centroid: nil) | ||
|
||
expect { task.invoke }.to change { location_polygon.reload.centroid }.from(nil).to( | ||
RGeo::Geographic.spherical_factory(srid: 4326).parse_wkt(centroid), | ||
) | ||
end | ||
|
||
it "does not update the centroid if it is already set" do | ||
location_polygon = create(:location_polygon, area: area, centroid: centroid) | ||
|
||
expect { task.invoke }.not_to(change { location_polygon.reload.centroid }) | ||
end | ||
|
||
it "does not update the centroid if the area is nil" do | ||
location_polygon = create(:location_polygon, area: nil, centroid: nil) | ||
|
||
expect { task.invoke }.not_to(change { location_polygon.reload.centroid }) | ||
end | ||
end |