Merge pull request #5594 from tchak/fix-geometry-errors

Fix geometry errors
This commit is contained in:
Paul Chavard 2020-09-22 09:38:53 +02:00 committed by GitHub
commit b4ae7e4492
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 22 deletions

View file

@ -45,12 +45,20 @@ module ChampHelper
"Culture : #{geo_area.culture} - Surface : #{geo_area.surface} ha"
when GeoArea.sources.fetch(:selection_utilisateur)
if geo_area.polygon?
capture do
concat "Une aire de surface #{geo_area.area} m"
concat content_tag(:sup, "2")
if geo_area.area.present?
capture do
concat "Une aire de surface #{geo_area.area} m"
concat content_tag(:sup, "2")
end
else
"Une aire de surface inconnue"
end
elsif geo_area.line?
"Une ligne longue de #{geo_area.length} m"
if geo_area.length.present?
"Une ligne longue de #{geo_area.length} m"
else
"Une ligne de longueur inconnue"
end
elsif geo_area.point?
"Un point situé à #{geo_area.location}"
end

View file

@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
import mapboxgl from 'mapbox-gl';
import ReactMapboxGl, { GeoJSONLayer, ZoomControl } from 'react-mapbox-gl';
import DrawControl from 'react-mapbox-gl-draw';
import { gpx, kml } from '@tmcw/togeojson/dist/togeojson.es.js';
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
import { getJSON, ajax, fire } from '@utils';
@ -11,7 +10,11 @@ import { getJSON, ajax, fire } from '@utils';
import { getMapStyle, SwitchMapStyle } from '../MapStyles';
import SearchInput from './SearchInput';
import { polygonCadastresFill, polygonCadastresLine } from './utils';
import {
polygonCadastresFill,
polygonCadastresLine,
readGeoFile
} from './utils';
import {
noop,
filterFeatureCollection,
@ -149,19 +152,7 @@ function MapEditor({ featureCollection, url, preview, options }) {
}
const onFileImport = (e, inputId) => {
const isGpxFile = e.target.files[0].name.includes('.gpx');
let reader = new FileReader();
reader.readAsText(e.target.files[0], 'UTF-8');
reader.onload = async (event) => {
let featureCollection;
isGpxFile
? (featureCollection = gpx(
new DOMParser().parseFromString(event.target.result, 'text/xml')
))
: (featureCollection = kml(
new DOMParser().parseFromString(event.target.result, 'text/xml')
));
readGeoFile(e.target.files[0]).then(async (featureCollection) => {
const resultFeatureCollection = await getJSON(
`${url}/import`,
featureCollection,
@ -196,7 +187,7 @@ function MapEditor({ featureCollection, url, preview, options }) {
updateFeaturesList(resultFeatureCollection.features);
setImportInputs(setInputs);
setBbox(resultFeatureCollection.bbox);
};
});
};
const addInputFile = (e) => {

View file

@ -1,3 +1,5 @@
import { gpx, kml } from '@tmcw/togeojson/dist/togeojson.es.js';
export const polygonCadastresFill = {
'fill-color': '#EC3323',
'fill-opacity': 0.3
@ -8,3 +10,81 @@ export const polygonCadastresLine = {
'line-width': 4,
'line-dasharray': [1, 1]
};
export function normalizeFeatureCollection(featureCollection) {
const features = [];
for (const feature of featureCollection.features) {
switch (feature.geometry.type) {
case 'MultiPoint':
for (const coordinates of feature.geometry.coordinates) {
features.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates
},
properties: feature.properties
});
}
break;
case 'MultiLineString':
for (const coordinates of feature.geometry.coordinates) {
features.push({
type: 'Feature',
geometry: {
type: 'LineString',
coordinates
},
properties: feature.properties
});
}
break;
case 'MultiPolygon':
for (const coordinates of feature.geometry.coordinates) {
features.push({
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates
},
properties: feature.properties
});
}
break;
case 'GeometryCollection':
for (const geometry of feature.geometry.geometries) {
features.push({
type: 'Feature',
geometry,
properties: feature.properties
});
}
break;
default:
features.push(feature);
}
}
featureCollection.features = features;
return featureCollection;
}
export function readGeoFile(file) {
const isGpxFile = file.name.includes('.gpx');
const reader = new FileReader();
return new Promise((resolve) => {
reader.onload = (event) => {
const xml = new DOMParser().parseFromString(
event.target.result,
'text/xml'
);
const featureCollection = normalizeFeatureCollection(
isGpxFile ? gpx(xml) : kml(xml)
);
resolve(featureCollection);
};
reader.readAsText(file, 'UTF-8');
});
}

View file

@ -79,13 +79,13 @@ class GeoArea < ApplicationRecord
def area
if polygon? && RGeo::Geos.supported?
rgeo_geometry.area.round(1)
rgeo_geometry.area&.round(1)
end
end
def length
if line? && RGeo::Geos.supported?
rgeo_geometry.length.round(1)
rgeo_geometry.length&.round(1)
end
end

View file

@ -5,6 +5,7 @@ namespace :after_party do
geometry_collections = GeoArea.where("geometry -> 'type' = '\"GeometryCollection\"'")
multi_polygons = GeoArea.where("geometry -> 'type' = '\"MultiPolygon\"'")
multi_line_strings = GeoArea.where("geometry -> 'type' = '\"MultiLineString\"'")
def valid_geometry?(geometry)
RGeo::GeoJSON.decode(geometry.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
@ -26,6 +27,24 @@ namespace :after_party do
end
progress.finish
progress = ProgressReport.new(multi_line_strings.count)
multi_line_strings.find_each do |multi_line_string|
multi_line_string.geometry['coordinates'].each do |coordinates|
geometry = {
type: 'LineString',
coordinates: coordinates
}
if valid_geometry?(geometry)
multi_line_string.champ.geo_areas.create!(geometry: geometry, source: 'selection_utilisateur')
end
end
multi_line_string.destroy
progress.inc
end
progress.finish
progress = ProgressReport.new(multi_polygons.count)
multi_polygons.find_each do |multi_polygon|
multi_polygon.geometry['coordinates'].each do |coordinates|