diff --git a/app/commands/decidim/admin/create_scope.rb b/app/commands/decidim/admin/create_scope.rb index 52ee2db..3c95b57 100644 --- a/app/commands/decidim/admin/create_scope.rb +++ b/app/commands/decidim/admin/create_scope.rb @@ -54,12 +54,20 @@ def create_scope def geojson return nil if form.geolocalized.blank? + geojson = form.geojson.deep_dup.deep_symbolize_keys { - color: form.geojson[:color], - geometry: form.geojson[:geometry], - parsed_geometry: form.geojson[:parsed_geometry] + color: geojson[:color] || form.color, + geometry: geojson[:geometry], + parsed_geometry: parsed_geometry(geojson) } end + + def parsed_geometry(geojson) + return if geojson.nil? || geojson[:geometry].nil? + return geojson[:parsed_geometry].deep_symbolize_keys if geojson[:parsed_geometry].is_a? Hash + + JSON.parse(geojson[:parsed_geometry].gsub("=>", ":")).deep_symbolize_keys + end end end end diff --git a/app/commands/decidim/admin/update_scope.rb b/app/commands/decidim/admin/update_scope.rb index 817cd6f..4221862 100644 --- a/app/commands/decidim/admin/update_scope.rb +++ b/app/commands/decidim/admin/update_scope.rb @@ -56,12 +56,20 @@ def attributes def geojson return nil if form.geolocalized.blank? + geojson = form.geojson.deep_dup.deep_symbolize_keys { - color: form.geojson[:color], - geometry: form.geojson[:geometry], - parsed_geometry: form.geojson[:parsed_geometry] + color: geojson[:color] || form.color, + geometry: geojson[:geometry], + parsed_geometry: parsed_geometry(geojson) } end + + def parsed_geometry(geojson) + return if geojson.nil? || geojson[:geometry].nil? + return geojson[:parsed_geometry].deep_symbolize_keys if geojson[:parsed_geometry].is_a? Hash + + JSON.parse(geojson[:parsed_geometry].gsub("=>", ":")).deep_symbolize_keys + end end end end diff --git a/lib/decidim/homepage_interactive_map/coordinates_swapper.rb b/lib/decidim/homepage_interactive_map/coordinates_swapper.rb index 4c7d0d3..aea7f0f 100644 --- a/lib/decidim/homepage_interactive_map/coordinates_swapper.rb +++ b/lib/decidim/homepage_interactive_map/coordinates_swapper.rb @@ -9,22 +9,20 @@ module HomepageInteractiveMap module CoordinatesSwapper def self.convert_geojson(geojson, opts = {}) return if geojson.nil? - return if geojson.try(:values)&.compact.blank? from = opts[:from] || detect_crs(geojson) || "EPSG:4326" to = opts[:to] || "EPSG:4326" - geojson_clone = geojson.dup.deep_symbolize_keys - new_coordinates = transform(geojson_clone[:parsed_geometry][:geometry][:coordinates], from, to) - new_geometry = geojson_clone[:parsed_geometry][:geometry].merge( + new_coordinates = transform(geojson[:parsed_geometry][:geometry][:coordinates], from, to) + new_geometry = geojson[:parsed_geometry][:geometry].merge( { coordinates: new_coordinates, crs: to } ) - new_parsed_geometry = geojson_clone[:parsed_geometry].merge(geometry: new_geometry) + new_parsed_geometry = geojson[:parsed_geometry].merge(geometry: new_geometry) - geojson_clone.merge(parsed_geometry: new_parsed_geometry) + geojson.merge(parsed_geometry: new_parsed_geometry) end def self.transform(coordinates, from, to) @@ -54,7 +52,7 @@ def self.coord_sys(coord_sys) end def self.detect_crs(geojson) - geojson.dup.deep_symbolize_keys.dig(:parsed_geometry, :geometry, :crs) + geojson.dig(:parsed_geometry, :geometry, :crs) end end end