|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<title>Fill space between two points and polygons | CartoDB.js</title> |
|
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> |
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> |
|
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" /> |
|
<style> |
|
html, body, #map { |
|
height: 100%; |
|
padding: 0; |
|
margin: 0; |
|
} |
|
</style> |
|
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.14/themes/css/cartodb.css" /> |
|
<link rel="stylesheet" href="http://leaflet.github.io/Leaflet.draw/leaflet.draw.css"> |
|
</head> |
|
<body> |
|
<div id="map"></div> |
|
|
|
|
|
<!-- include cartodb.js library and the leaflet draw plugin--> |
|
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.14/cartodb.js"></script> |
|
<script src="http://leaflet.github.io/Leaflet.draw/leaflet.draw.js"></script> |
|
<script> |
|
function main() { |
|
|
|
var sql = new cartodb.SQL({user: 'jsanz'}); |
|
// Create a new map |
|
var map = L.map('map', { |
|
zoomControl: true, |
|
center: [40.43452737429919,-3.699088096618652], |
|
zoom: 16 |
|
}); |
|
|
|
// Set up the base layer |
|
basemap = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', { |
|
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>' |
|
}); |
|
basemap.addTo(map); |
|
|
|
// Layer for the parameters |
|
var editableLayers = new L.FeatureGroup(); |
|
map.addLayer(editableLayers); |
|
|
|
// Arrays for the features |
|
var polygons = [], markers = []; |
|
|
|
// Layer for the results |
|
var resultLayer = L.geoJson().addTo(map); |
|
|
|
// Set up the draw control |
|
var drawControl = new L.Control.Draw({ |
|
draw: { |
|
polyline: false, |
|
polygon: { |
|
allowIntersection: false, // Restricts shapes to simple polygons |
|
shapeOptions: {color: '#bada55'} |
|
}, |
|
circle: false, |
|
rectangle: false |
|
}, |
|
edit: { |
|
featureGroup: editableLayers |
|
} |
|
}); |
|
map.addControl(drawControl); |
|
|
|
// When a new feature is added put it also on the arrays |
|
map.on('draw:created', function (e) { |
|
var type = e.layerType, |
|
layer = e.layer; |
|
|
|
if (type === 'polygon'){ |
|
polygons.push(layer); |
|
if (polygons.length == 3){ |
|
editableLayers.removeLayer(polygons.shift()); |
|
|
|
} |
|
} else { |
|
markers.push(layer); |
|
if (markers.length == 3){ |
|
editableLayers.removeLayer(markers.shift()); |
|
} |
|
} |
|
|
|
editableLayers.addLayer(layer); |
|
}); |
|
|
|
// When a new feature is added, try to execute the procedure |
|
editableLayers.on('layeradd',function(e){ |
|
var sqlStm = "select ST_AsGeoJSON(fillempty(ST_GeomFromText(\'{{g1}}\'),ST_GeomFromText(\'{{g2}}\'),ST_GeomFromText(\'{{p1}}\'),ST_GeomFromText(\'{{p2}}\'),{{tol}})) as result"; |
|
|
|
// Only execute the procedure if the arrays are full |
|
if (polygons.length == 2 && markers.length == 2){ |
|
// SQL Options object |
|
var options = { |
|
g1:getPolWKT(polygons[0]), |
|
g2:getPolWKT(polygons[1]), |
|
p1:getPointWKT(markers[0]), |
|
p2:getPointWKT(markers[1]), |
|
tol: 0.00001 // this parameter really depends on the scale of your features |
|
} |
|
|
|
// Execute the SQL |
|
sql.execute(sqlStm,options) |
|
.done(function(data){ |
|
// Remove the previous result |
|
resultLayer.removeLayer(resultLayer.getLayers()[0]); |
|
// Add the new geoJSON object |
|
var geom = data.rows[0].result; |
|
resultLayer.addData(eval('('+geom+')')); |
|
}) |
|
.error(function(errors){ |
|
console.log(errors); |
|
}); |
|
} |
|
}); |
|
} |
|
|
|
// Utility function to generate a Poligon WKT from a GeoJSON |
|
function getPolWKT(polygon){ |
|
return 'POLYGON ((' + |
|
polygon.toGeoJSON().geometry.coordinates[0].map(function(p){ |
|
return p[0] + ' ' + p[1]}) |
|
.join(',') + '))'; |
|
} |
|
// Utility function to generate a Point WKT from a GeoJSON |
|
function getPointWKT(point){ |
|
return 'POINT (' + point.toGeoJSON().geometry.coordinates.join(' ') + ')'; |
|
} |
|
|
|
window.onload = main; |
|
</script> |
|
</body> |
|
</html> |