-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Fmu): Download shapefiles from Admin
Allows downloading the FMU's shapefiles.
- Loading branch information
1 parent
9465e0e
commit 207e7cf
Showing
12 changed files
with
171 additions
and
7 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
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
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
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
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,69 @@ | ||
require "gdal" | ||
require "rgeo" | ||
require "zip" | ||
|
||
class ShapefileService | ||
# Generate a shapefile from a collection of objects that respond to the `geometry` method | ||
# The `geometry` method should return an RGeo::Feature object | ||
def self.generate_shapefile(shapes) | ||
# Create a temporary directory to store the shapefile components | ||
Dir.mktmpdir do |dir| | ||
shapes_name = (shapes.size == 1) ? shapes.first.name : "shapes" | ||
|
||
shapefile_path = File.join(dir, "#{shapes_name}.shp") | ||
|
||
# Initialize GDAL | ||
driver = Gdal::Ogr.get_driver_by_name("ESRI Shapefile") | ||
datasource = driver.create_data_source(shapefile_path) | ||
layer = datasource.create_layer("shapes", nil, Gdal::Ogr::WKBPOLYGON) | ||
|
||
# Define the fields | ||
field_defn = Gdal::Ogr::FieldDefn.new("id", Gdal::Ogr::OFTINTEGER) | ||
layer.create_field(field_defn) | ||
field_defn = Gdal::Ogr::FieldDefn.new("name", Gdal::Ogr::OFTSTRING) | ||
layer.create_field(field_defn) | ||
field_defn = Gdal::Ogr::FieldDefn.new("operator", Gdal::Ogr::OFTSTRING) | ||
layer.create_field(field_defn) | ||
|
||
shapes.each do |shape| | ||
feature = Gdal::Ogr::Feature.new(layer.get_layer_defn) | ||
feature.set_field("id", shape.id) | ||
feature.set_field("name", shape.name) | ||
feature.set_field("operator", shape.operator&.name) | ||
geometry = Gdal::Ogr.create_geometry_from_wkt(shape.geometry.as_text) | ||
feature.set_geometry(geometry) | ||
layer.create_feature(feature) | ||
end | ||
|
||
datasource.sync_to_disk | ||
|
||
# Write the .prj file | ||
prj_content = generate_prj_content | ||
prj_path = File.join(dir, "#{shapes_name}.prj") | ||
File.write(prj_path, prj_content) | ||
|
||
# Collect the generated shapefile parts | ||
files = Dir.glob("#{dir}/*") | ||
|
||
zipfile_path = File.join(dir, "#{shapes_name}.zip") | ||
Zip::File.open(zipfile_path, Zip::File::CREATE) do |zipfile| | ||
files.each do |file| | ||
zipfile.add(File.basename(file), file) | ||
end | ||
end | ||
|
||
# Read the zipfile content | ||
zip_content = File.read(zipfile_path) | ||
|
||
# Return the zip content | ||
zip_content | ||
end | ||
end | ||
|
||
def self.generate_prj_content | ||
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,' \ | ||
'AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,' \ | ||
'AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,' \ | ||
'AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]' | ||
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
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,22 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe ShapefileService do | ||
describe ".generate_shapefile" do | ||
let(:fmu1) { create(:fmu_geojson) } | ||
let(:fmu2) { create(:fmu_geojson) } | ||
|
||
it "returns a zip file with the shapefile components" do | ||
file_content = described_class.generate_shapefile([fmu1.reload, fmu2.reload]) | ||
zip_file = Tempfile.new("shapes.zip") | ||
zip_file.write(file_content) | ||
zip_file.rewind | ||
|
||
Zip::File.open(zip_file) do |zip| | ||
expect(zip.glob("*.shp").count).to eq(1) | ||
expect(zip.glob("*.shx").count).to eq(1) | ||
expect(zip.glob("*.dbf").count).to eq(1) | ||
expect(zip.glob("*.prj").count).to eq(1) | ||
end | ||
end | ||
end | ||
end |