Skip to content
This repository has been archived by the owner. It is now read-only.

introduce app logic to warn users when they create triggers less than 50 sq/m #225

Merged
merged 3 commits into from Sep 29, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -274,10 +274,14 @@ <h2>Geotrigger Editor</h2>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.marionette/1.4.1-bundled/backbone.marionette.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.runtime.min.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.2.3/leaflet.draw.js"></script>
<script src="//cdn.jsdelivr.net/leaflet.esri/1.0.1/esri-leaflet.js"></script>
<script src="//cdn.jsdelivr.net/leaflet.esri.geocoder/1.0.2/esri-leaflet-geocoder.js"></script>
<script src="//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js"></script>

<!-- js: local dependencies -->
<!-- js: dependencies -->
<script src="https://cdn.rawgit.com/Esri/geotrigger-js/10027b9c663c00a6d9faf776d97e9c3e546c5780/geotrigger.min.js"></script>
<script src="//cdn.jsdelivr.net/leaflet.esri/1.0.4/esri-leaflet.js"></script>
<script src="//cdn.jsdelivr.net/leaflet.esri.geocoder/1.0.2/esri-leaflet-geocoder.js"></script>
@@ -55,7 +55,33 @@ Geotrigger.Editor.module('Map.Draw', function (Draw, App, Backbone, Marionette,
broadcastLayer: function (e) {
var type = e.layerType;
var layer = e.layer;
var triggerArea;

if (type == 'circle') {
var origin = layer.getLatLng(); //center of drawn circle
var radius = layer.getRadius(); //radius of drawn circle
var projection = L.CRS.EPSG4326;
var polys = App.util.createGeodesicPolygon(origin, radius, 60, 0, projection); //these are the points that make up the circle
var polygon = []; // store the geometry
for (var i = 0; i < polys.length; i++) {
var geometry = [polys[i].lat, polys[i].lng];
polygon.push(geometry);
}
triggerArea = turf.area(L.polygon(polygon).toGeoJSON());
}

else {
triggerArea = turf.area(e.layer.toGeoJSON());
}

if (triggerArea < 2500) {
App.vent.trigger('notify', 'we recommend triggers have an area greater than 50m&#178;');

window.setTimeout(function () {
App.vent.trigger('notify:clear');
}, 5000);

}
App.vent.trigger('draw:new', layer);
},

@@ -52,4 +52,74 @@ Geotrigger.Editor.module('util', function (util, App, Backbone, Marionette, $, _
return Object.prototype.toString.call(obj) === '[object Array]';
};

util.createGeodesicPolygon = function (origin, radius, sides, rotation, projection) {
var latlon = origin; //leaflet equivalent
var angle;
var new_lonlat, geom_point;
var points = [];

for (var i = 0; i < sides; i++) {
angle = (i * 360 / sides) + rotation;
new_lonlat = this.destinationVincenty(latlon, angle, radius);
geom_point = L.latLng(new_lonlat.lng, new_lonlat.lat);

points.push(geom_point);
}

return points;
};

/* cribbed from
http://stackoverflow.com/questions/24145205/writing-a-function-to-convert-a-circle-to-a-polygon-using-leaflet-js
*/
util.destinationVincenty = function (lonlat, brng, dist) {
var cos2SigmaM, sinSigma, cosSigma, deltaSigma;
// var u = L.Util;
var ct = this.VincentyConstants;
var a = ct.a, b = ct.b, f = ct.f;
var lon1 = lonlat.lng;
var lat1 = lonlat.lat;
var s = dist;
var pi = Math.PI;
var alpha1 = brng * pi/180 ; //converts brng degrees to radius
var sinAlpha1 = Math.sin(alpha1);
var cosAlpha1 = Math.cos(alpha1);
var tanU1 = (1-f) * Math.tan( lat1 * pi/180 /* converts lat1 degrees to radius */ );
var cosU1 = 1 / Math.sqrt((1 + tanU1*tanU1)), sinU1 = tanU1*cosU1;
var sigma1 = Math.atan2(tanU1, cosAlpha1);
var sinAlpha = cosU1 * sinAlpha1;
var cosSqAlpha = 1 - sinAlpha*sinAlpha;
var uSq = cosSqAlpha * (a*a - b*b) / (b*b);
var A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
var B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
var sigma = s / (b*A), sigmaP = 2*Math.PI;
while (Math.abs(sigma-sigmaP) > 1e-12) {
cos2SigmaM = Math.cos(2*sigma1 + sigma);
sinSigma = Math.sin(sigma);
cosSigma = Math.cos(sigma);
deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-
B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
sigmaP = sigma;
sigma = s / (b*A) + deltaSigma;
}
var tmp = sinU1*sinSigma - cosU1*cosSigma*cosAlpha1;
var lat2 = Math.atan2(sinU1*cosSigma + cosU1*sinSigma*cosAlpha1,
(1-f)*Math.sqrt(sinAlpha*sinAlpha + tmp*tmp));
var lambda = Math.atan2(sinSigma*sinAlpha1, cosU1*cosSigma - sinU1*sinSigma*cosAlpha1);
var C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
var lam = lambda - (1-C) * f * sinAlpha *
(sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
var revAz = Math.atan2(sinAlpha, -tmp); // final bearing
var lamFunc = lon1 + (lam * 180/pi); //converts lam radius to degrees
var lat2a = lat2 * 180/pi; //converts lat2a radius to degrees

return L.latLng(lamFunc, lat2a);
};

util.VincentyConstants = {
a: 6378137,
b: 6356752.3142,
f: 1/298.257223563
};

});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.