Skip to content

Commit

Permalink
Update local authorities data
Browse files Browse the repository at this point in the history
  • Loading branch information
slawosz committed Nov 13, 2024
1 parent 2d73d9f commit 9698d2b
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 14 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ This project depends on:

When updating the `db/data/hubs.csv` file, please make sure to run `rails r 'Hub::Importer.new.reload!'` locally first to make sure all changes were applied.

### Updating Authorities File

Geojson file can be find here: https://geoportal.statistics.gov.uk/datasets/f23beaa3769a4488b6a5f0cfb7980f51_0/explore?location=52.427623%2C-1.470139%2C8.26
Due to size of the file, it is split into several files. The example code to split the file:

```ruby
file = Rails.root.join("db/data/authorities.json").freeze

size = 80
5.times do |i|
data = JSON(File.read(file))
data["features"] = data["features"][(size*i)...(size*(i+1))]

filename = Rails.root.join("db/data/authorities_#{i+1}.json")
File.open(filename, 'w') { |file| file.write(data.to_json) }
end
```

## Testing

### Mocking with VCR for feature specs
Expand Down
22 changes: 12 additions & 10 deletions app/lib/local_authority/importer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class LocalAuthority::Importer
DEFAULT_SOURCE = Rails.root.join("db/data/authorities.json").freeze
DEFAULT_SOURCES = (1..5).map { |num| Rails.root.join("db/data/authorities_#{num}.json") }.freeze
attr_reader :path

def initialize(path = DEFAULT_SOURCE)
@path = path
def initialize(paths = DEFAULT_SOURCES)
@paths= paths
end

def reload!
Expand All @@ -29,15 +29,17 @@ def read_file(geojson_path)
end

def load
Rails.logger.debug("Loading #{@path}")
@paths.each do |path|
Rails.logger.debug("Loading #{path}")

features = read_file(@path)
features = read_file(path)

features.each do |feature|
LocalAuthority.create!(
name: feature.properties["LAD22NM"],
geometry: feature.geometry
)
features.each do |feature|
LocalAuthority.create!(
name: feature.properties["LAD24NM"],
geometry: feature.geometry
)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/local_authority/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def search_polygon

def transform(geo)
RGeo::CoordSys::Proj4.transform(
RGeo::CoordSys::Proj4.new("EPSG:4326"),
RGeo::CoordSys::Proj4.new("EPSG:27700"),
geo,
RGeo::CoordSys::Proj4.new("EPSG:27700"),
RGeo::Cartesian.preferred_factory
Expand Down
1 change: 0 additions & 1 deletion db/data/authorities.json

This file was deleted.

1 change: 1 addition & 0 deletions db/data/authorities_1.json

Large diffs are not rendered by default.

Loading

0 comments on commit 9698d2b

Please sign in to comment.