Skip to content

Commit b7aaeb8

Browse files
committed
Reproject and transform crop mask to geojson and then correctly zoom into the preview map if the map has been cropped :emoji: refs #94
1 parent 7c21e0b commit b7aaeb8

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

app/assets/javascripts/warped.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ function warpedinit() {
6666

6767
clipmap_bounds_merc = warped_bounds.transform(warpedmap.displayProjection, warpedmap.projection);
6868

69-
warpedmap.zoomToExtent(clipmap_bounds_merc);
69+
if (mask_geojson) {
70+
var vector = new OpenLayers.Layer.Vector("GeoJSON", {
71+
projection: "EPSG:4326"
72+
});
73+
var gformat = new OpenLayers.Format.GeoJSON();
74+
vector.addFeatures(gformat.read(mask_geojson));
75+
warpedmap.zoomToExtent(vector.getDataExtent());
76+
} else {
77+
warpedmap.zoomToExtent(clipmap_bounds_merc);
78+
}
7079

7180
//set up slider
7281
jQuery("#slider").slider({

app/models/map.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ def mask!
612612
else
613613
return "no masking file matching specified format found."
614614
end
615+
616+
self.mask_geojson = convert_mask_to_geojson
615617

616618
masked_src_filename = self.masked_src_filename
617619
if File.exists?(masked_src_filename)
@@ -668,6 +670,8 @@ def warp!(resample_option, transform_option, use_mask="false")
668670
if use_mask == "true" && self.mask_status == :masked
669671
src_filename = self.masked_src_filename
670672
mask_options = " -srcnodata '17 17 17' "
673+
674+
self.mask_geojson = convert_mask_to_geojson if self.mask_geojson.blank?
671675
else
672676
src_filename = self.unwarped_filename
673677
end
@@ -966,6 +970,32 @@ def clear_cache
966970
Rails.cache.delete_matched ".*/maps/wms/#{self.id}.png\?status=warped.*"
967971
Rails.cache.delete_matched "*/maps/tile/#{self.id}/*"
968972
end
973+
974+
#takes in the clipping mask file, transforms it to geo and converts to geojson, returning the geojson
975+
def convert_mask_to_geojson
976+
if self.gcps.hard.size < 3
977+
return nil;
978+
else
979+
gcp_array = self.gcps.hard
980+
gcp_string = ""
981+
gcp_array.each do |gcp|
982+
gcp_string = gcp_string + gcp.gdal_string
983+
end
984+
985+
command = "ogr2ogr -f 'geojson' -s_srs 'epsg:4326' -t_srs 'epsg:3857' #{gcp_string} /dev/stdout #{self.masking_file_gml}"
986+
logger.info command
987+
o_out, o_err = Open3.capture3( command )
988+
989+
if !o_err.blank?
990+
logger.error "Error ogr2ogr script" + o_err
991+
logger.error "output = "+o_out
992+
return nil;
993+
end
994+
995+
996+
return o_out
997+
end
998+
end
969999

9701000

9711001
end

app/views/maps/warped.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
<script type="text/javascript">
2222
var map_id = '<%=h @map.id -%>';
2323
var title = '<%=u @map.title -%>';
24-
var warped_image_width = <%= @map.width %>
25-
var warped_image_height = <%= @map.height %>
24+
var warped_image_width = <%= @map.width %>;
25+
var warped_image_height = <%= @map.height %>;
2626
var warpedwms_url = '<%= url_for :controller => 'maps', :action => 'wms', :id => @map -%>';
2727
var warped_bounds = new OpenLayers.Bounds(<%=@map.bounds-%>);
2828
var layer_baseurl = '<%=url_for :controller => 'layers', :action => 'wms' %>';
2929
var layers_array = [<%=@other_layers.join(",")-%>];
3030
var warpedtiles_url = '<%= tile_map_base_url %>/';
3131
var use_tiles = <%= !user_signed_in? %>;
32+
var mask_geojson = <%= @map.mask_status != :masked || @map.mask_geojson.blank? ? false : @map.mask_geojson.html_safe %>;
3233
</script>
3334

3435

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddMaskGeojsonToMap < ActiveRecord::Migration
2+
def change
3+
add_column :maps, :mask_geojson, :text
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20190221161545) do
14+
ActiveRecord::Schema.define(version: 20190228170210) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -207,6 +207,7 @@
207207
t.datetime "gcp_touched_at"
208208
t.integer "issue_year"
209209
t.boolean "protect", default: false
210+
t.text "mask_geojson"
210211
end
211212

212213
add_index "maps", ["bbox_geom"], name: "index_maps_on_bbox_geom", using: :gist

test/models/map_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ class MapTest < ActiveSupport::TestCase
4141
end
4242
end
4343

44+
test "converts mask to geojson" do
45+
test_gml = File.join(Rails.root, "/test/fixtures/data/test.gml")
46+
Map.any_instance.stubs(:masking_file_gml).returns(test_gml)
47+
@map.mask!
48+
49+
assert_not_nil @map.mask_geojson
50+
json = JSON.parse(@map.mask_geojson)
51+
assert "FeatureCollection", json["type"]
52+
end
53+
4454
private
4555

4656
def delete_created_images(map)

0 commit comments

Comments
 (0)