node-red-contrib-snap4city-user
Version:
Nodes for Snap4city project, targeted to standard user (no developer)
284 lines (257 loc) • 11.3 kB
HTML
<!--/* NODE-RED-CONTRIB-SNAP4CITY-USER
Copyright (C) 2018 DISIT Lab http://www.disit.org - University of Florence
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */-->
<script type="text/javascript">
RED.nodes.registerType('bus-routes-search-within-circle', {
category: 'S4CSearch',
color: '#C7E9C0',
defaults: {
name: {
value: ""
},
authentication: {
type: "snap4city-authentication",
required: false
},
latitude: {
value: 0.0,
required: false,
validate: RED.validators.number()
},
longitude: {
value: 0.0,
required: false,
validate: RED.validators.number()
},
agency: {
value: "",
required: false
},
maxdists: {
value: 1,
required: false,
validate: RED.validators.number()
},
maxresults: {
value: 100,
required: false,
validate: RED.validators.number()
}
},
outputs: 1,
inputs: 1,
outputLabels: ["routes"],
icon: "tpl.png",
label: function () {
return this.name || "bus-routes-search-within-circle";
},
oneditprepare: function () {
$.ajax({
url: "s4c/js/leaflet.js",
async: false,
dataType: "script"
});
$.ajax({
url: "s4c/js/leaflet.draw.js",
async: false,
dataType: "script"
});
node = this;
map = L.map('node-input-map').setView([43.78, 11.23], 9);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
window.node_input_map = map;
var mapLayers = {};
drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
var editControl = new L.Control.Draw({
draw: false,
edit: {
featureGroup: drawnItems
}
});
map.addControl(editControl);
drawControl = new L.Control.Draw({
draw: {
position: 'topleft',
polyline: false,
marker: false,
circlemarker: false,
polygon: false,
rectangle: false
}
});
map.addControl(drawControl);
L.control.layers(mapLayers, {
'drawlayer': drawnItems
}, {
collapsed: true
}).addTo(map);
map.on(L.Draw.Event.CREATED, function (e) {
var fence = e.layer;
fence.nodeID = node.id;
if (drawnItems.hasLayer(fence) == false) {
drawnItems.addLayer(fence);
}
drawControl.remove();
circles = {};
drawnItems.eachLayer(function (layer) {
circles[layer.nodeID] = layer.toGeoJSON();
circles[layer.nodeID].properties.radius = Math.round(layer.getRadius()) /
1000;
});
$("#node-input-latitude").val(circles[node.id].geometry.coordinates[1]);
$("#node-input-longitude").val(circles[node.id].geometry.coordinates[0]);
$("#node-input-maxdists").val(circles[node.id].properties.radius);
});
map.on('draw:edited', function (e) {
var fences = e.layers;
fences.eachLayer(function (fence) {
fence.shape = "geofence";
if (drawnItems.hasLayer(fence) == false) {
drawnItems.addLayer(fence);
}
});
circles = {};
drawnItems.eachLayer(function (layer) {
circles[layer.nodeID] = layer.toGeoJSON();
circles[layer.nodeID].properties.radius = layer.getRadius();
});
$("#node-input-latitude").val(circles[node.id].geometry.coordinates[1]);
$("#node-input-longitude").val(circles[node.id].geometry.coordinates[0]);
$("#node-input-maxdists").val(circles[node.id].properties.radius);
});
map.on('draw:deleted', function (e) {
drawControl.addTo(map);
$("#node-input-latitude").val(0);
$("#node-input-longitude").val(0);
$("#node-input-maxdists").val(100);
});
$("#node-input-agency").empty();
var domain = (RED.nodes.node($("#node-input-authentication").val()) != null ? RED.nodes.node($("#node-input-authentication").val()).domain: "");
$.ajax({
url: (domain ? domain : (RED.settings.ascapiUrl ? RED.settings.ascapiUrl : "https://www.snap4city.org/")) + "/superservicemap/api/v1/tpl/agencies/",
type: "GET",
async: false,
dataType: "json",
success: function (data) {
var agencies = data.Agencies;
for (var i = 0; i < agencies.length; i++) {
$("<option value=\"" + agencies[i].agency + "\">" + agencies[i].name +
"</option>").appendTo($("#node-input-agency"));
}
$("#node-input-agency").val("");
},
error: function (err) {
console.log(err);
}
});
},
oneditresize: function () {
if (window.node_input_map) {
window.node_input_map.invalidateSize(true);
}
}
});
</script>
<script type="text/x-red" data-template-name="bus-routes-search-within-circle">
<div class="form-row" id="rowAuthentication">
<label for="node-input-authentication">Authentication</label>
<input type="text" id="node-input-authentication">
</div>
<div class="form-tips" id="tipAuthentication" style="margin-bottom: 10px;">
If you have private data and you want to access them, you must have to insert you account data. You can register for one account
<a href="https://www.snap4city.org"
target="_blank">here</a>.
</div>
<div class="form-row">
<label for="node-input-name">Name</label>
<input type="text" autocomplete="off" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-maxresults">Max Results</label>
<select id="node-input-maxresults">
<option value="100">100</option>
<option value="200">200</option>
<option value="500">500</option>
<option value="1000">1000</option>
<option value="2000">2000</option>
<option value="0">All</option>
</select>
</div>
<div class="form-row">
<label for="node-input-agency">Agency</label>
<select id="node-input-agency">
</select>
</div>
<div class="form-row">
<label for="node-input-latitude">Latitude</label>
<input type="text" autocomplete="off" id="node-input-latitude" placeholder="Latitude" disabled>
</div>
<div class="form-row">
<label for="node-input-longitude">Longitude</label>
<input type="text" autocomplete="off" id="node-input-longitude" placeholder="Longitude" disabled>
</div>
<div class="form-row">
<label for="node-input-maxdists">Max Distance</label>
<input type="text" autocomplete="off" id="node-input-maxdists" placeholder="Max Distance" disabled>
</div>
<div class="form-row">
<link rel="stylesheet" href="s4c/css/leaflet.css" />
<link rel="stylesheet" href="s4c/css/leaflet.draw.css" />
<div id="node-input-map" style="width: 80%; height: 300px"></div>
</div>
</script>
<script type="text/x-red" data-help-name="bus-routes-search-within-circle">
<p>It provides a list of the public transport routes that have a stop in a specified area.
</p>
<h3>Inputs</h3>
A JSON with these parameters:
<dl class="message-properties">
<dt>latitude
<span class="property-type">number</span>
</dt>
<dd> latitude of a GPS position</dd>
<dt>longitude
<span class="property-type">number</span>
</dt>
<dd> longitude of a GPS position</dd>
<dt>agency
<span class="property-type">string</span>
</dt>
<dd> URI of an agency to restrict the search to a specified agency
</dd>
<dt>maxdistance
<span class="property-type">number</span>
</dt>
<dd> maximum distance from the GPS position of the services to be retrieved, expressed in Km (0.1 is used if parameter
is missing) if it is equal to “inside” it searches for services with a WKT geometry that contains the specified
GPS position (e.g a park)</dd>
<dt>maxresults
<span class="property-type">number</span>
</dt>
<dd> maximum number of results to be returned (if parameter is missing 100 is assumed), if it is 0 all results are returned</dd>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<dd> It provides a JSON Object with all the routes that have stops on the specified area. For each route the following
properties are provided: • lineNumber: the line short name • lineName: the line long name • route: the route
name • routeUri: an URI identifying the route (it can be used to retrieve all the stops of the route) • direction:
with first and last stop • agency: with agency name • agencyUri: with agency URI • polyline: with the WKT geometry
of the route</dd>
</dl>
<h3>Details</h3>
<p>The node can receive a JSON with the parameters described in the Inputs section and with them generate the output JSON.
If the values are not present in the input JSON, these are read by those in the configuration. If they are not present
in either part, an error is generated for the necessary parameters.</p>
</script>