New turf area source
This commit is contained in:
parent
913905001e
commit
b4007c2735
4 changed files with 94 additions and 20 deletions
|
@ -25,7 +25,7 @@
|
|||
//= require graham_scan.min
|
||||
//= require leaflet.freedraw
|
||||
//= require smart_listing
|
||||
//= require turf
|
||||
//= require turf-area
|
||||
//= require franceconnect
|
||||
//= require bootstrap-wysihtml5
|
||||
//= require bootstrap-wysihtml5/locales/fr-FR
|
||||
|
|
|
@ -57,7 +57,7 @@ function get_external_data(latLngs) {
|
|||
|
||||
cadastre_list = [{zoom_error: true}];
|
||||
|
||||
if (turf.area(polygons) < 300000)
|
||||
if (turf_area(polygons) < 300000)
|
||||
cadastre_list = get_cadastre(latLngs);
|
||||
|
||||
display_cadastre(cadastre_list);
|
||||
|
|
92
vendor/assets/javascripts/turf-area.js
vendored
Normal file
92
vendor/assets/javascripts/turf-area.js
vendored
Normal file
|
@ -0,0 +1,92 @@
|
|||
var geometryArea = geometry;
|
||||
|
||||
turf_area = function(_) {
|
||||
if (_.type === 'FeatureCollection') {
|
||||
for (var i = 0, sum = 0; i < _.features.length; i++) {
|
||||
if (_.features[i].geometry) {
|
||||
sum += geometryArea(_.features[i].geometry);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
} else if (_.type === 'Feature') {
|
||||
return geometryArea(_.geometry);
|
||||
} else {
|
||||
return geometryArea(_);
|
||||
}
|
||||
};
|
||||
|
||||
RADIUS = 6378137;
|
||||
FLATTENING = 1/298.257223563;
|
||||
POLAR_RADIUS = 6356752.3142;
|
||||
|
||||
ring = ringArea;
|
||||
|
||||
function geometry(_) {
|
||||
var area = 0, i;
|
||||
switch (_.type) {
|
||||
case 'Polygon':
|
||||
return polygonArea(_.coordinates);
|
||||
case 'MultiPolygon':
|
||||
for (i = 0; i < _.coordinates.length; i++) {
|
||||
area += polygonArea(_.coordinates[i]);
|
||||
}
|
||||
return area;
|
||||
case 'Point':
|
||||
case 'MultiPoint':
|
||||
case 'LineString':
|
||||
case 'MultiLineString':
|
||||
return 0;
|
||||
case 'GeometryCollection':
|
||||
for (i = 0; i < _.geometries.length; i++) {
|
||||
area += geometry(_.geometries[i]);
|
||||
}
|
||||
return area;
|
||||
}
|
||||
}
|
||||
|
||||
function polygonArea(coords) {
|
||||
var area = 0;
|
||||
if (coords && coords.length > 0) {
|
||||
area += Math.abs(ringArea(coords[0]));
|
||||
for (var i = 1; i < coords.length; i++) {
|
||||
area -= Math.abs(ringArea(coords[i]));
|
||||
}
|
||||
}
|
||||
return area;
|
||||
}
|
||||
|
||||
function ringArea(coords) {
|
||||
var p1, p2, p3, lowerIndex, middleIndex, upperIndex,
|
||||
area = 0,
|
||||
coordsLength = coords.length;
|
||||
|
||||
if (coordsLength > 2) {
|
||||
for (i = 0; i < coordsLength; i++) {
|
||||
if (i === coordsLength - 2) {// i = N-2
|
||||
lowerIndex = coordsLength - 2;
|
||||
middleIndex = coordsLength -1;
|
||||
upperIndex = 0;
|
||||
} else if (i === coordsLength - 1) {// i = N-1
|
||||
lowerIndex = coordsLength - 1;
|
||||
middleIndex = 0;
|
||||
upperIndex = 1;
|
||||
} else { // i = 0 to N-3
|
||||
lowerIndex = i;
|
||||
middleIndex = i+1;
|
||||
upperIndex = i+2;
|
||||
}
|
||||
p1 = coords[lowerIndex];
|
||||
p2 = coords[middleIndex];
|
||||
p3 = coords[upperIndex];
|
||||
area += ( rad(p3[0]) - rad(p1[0]) ) * Math.sin( rad(p2[1]));
|
||||
}
|
||||
|
||||
area = area * RADIUS * RADIUS / 2;
|
||||
}
|
||||
|
||||
return area;
|
||||
}
|
||||
|
||||
function rad(_) {
|
||||
return _ * Math.PI / 180;
|
||||
}
|
18
vendor/assets/javascripts/turf.js
vendored
18
vendor/assets/javascripts/turf.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue