Commit 17be6f0a authored by Clément Igonet's avatar Clément Igonet

Add overpass loader.

parent bf60a0a2
integration.openearthview.net {
tls contact@openearthview.net
root /var/www/viewer-earth-api-integration
gzip
}
......@@ -45,13 +45,21 @@
<script src="node_modules/three/build/three.js"></script>
<script src="js/turf/v2.0.2/turf.min.js"></script>
<script src="js/openearthview.js" type="text/javascript"></script>
<script src="js/loaders/TileLoader.js" type="text/javascript"></script>
<script src="js/loaders/GeojsonLoader.js"></script>
<script src="js/toolbox.js" type="text/javascript"></script>
<!-- <script src="js/loaders/TerrainLoader.js" type="text/javascript"></script> -->
<script src="js/loaders/TileLoader.js" type="text/javascript"></script>
<script src="js/loaders/TerrainLoader.js" type="text/javascript"></script>
<!-- <script src="js/loaders/GeojsonLoader.js"></script> -->
<script src="js/loaders/OverpassJsonLoader.js"></script>
<!-- <script src="js/loaders/mapzenGeojsonLoader.js"></script> -->
<!-- <script src="js/toolbox.js" type="text/javascript"></script> -->
<script src="js/controls/EarthControls.js"></script>
<script src="js/layers/Layer.js"></script>
<script src="js/layers/OsmLayer.js"></script>
<script src="js/layers/BuildingLayer.js"></script>
<script src="js/layers/OverpassBuildingLayer.js"></script>
<script src="js/terrains/Terrain.js"></script>
<script src="js/terrains/FlatTerrain.js"></script>
<title>OpenEartView example</title>
</head>
......@@ -67,22 +75,93 @@
// "http://b.tile.openstreetmap.org/${z}/${x}/${y}.png",
// "http://c.tile.openstreetmap.org/${z}/${x}/${y}.png"
// ]));
world.addLayer(new OpenEarthView.Layer.Building(
"OpenEarthView", [
'http://localhost:8081/3dtile?format=geojson&xtile=${x}&ytile=${y}&zoom=${z}&factor=${f}'
]));
// world.addLayer(new OpenEarthView.Layer.Building(
// "OpenEarthView", [
// 'http://localhost:8081/3dtile?format=geojson&xtile=${x}&ytile=${y}&zoom=${z}&factor=${f}'
// ]));
world.addLayer(new OpenEarthView.Layer.OverpassBuilding(
"Overpass", [
'http://overpass-api.de/api/interpreter'
], {
minZoom: 18
}));
// curl -G -d 'data=[bbox];(relation["building"])->.relbuilding;'\
// '.relbuilding%20is_in({{center}})->.relcentered;'\
// '(.relcentered;>;)->.bigbuildings' \
// '(way["building"])->.waybuilding;'\
// '.waybuilding%20is_in({{center}})->.waycentered;'\
// '(.bigbuildings;.waycentered;);out;' \
// "http://overpass-api.de/api/interpreter?bbox=55.7523958,37.6216078,55.7528487,37.6237535"
// [out:json];
// (relation["building"]({{bbox}}))->.relbuilding;
// .relbuilding is_in({{center}})->.relcentered;
// (.relcentered;>;)->.bigbuildings;
// (way["building"]({{bbox}}))->.waybuilding;
// .waybuilding is_in({{center}})->.waycentered;
// (.bigbuildings;.waycentered;);
// out;
// http://localhost:8083/proxycache-geojson?tile=${z},${x},${y}
// world.addLayer(new OpenEarthView.Layer.Building(
// "OpenEarthView", [
// 'http://localhost:8083/proxycache-geojson?tile=${z},${x},${y}'
// ], {
// minZoom: 17
// }));
// world.addLayer(new OpenEarthView.Layer.Building(
// "OpenEarthView", [
// 'http://vector.mapzen.com/osm/buildings/${z}/${x}/${y}.json'
// ], {
// minZoom: 17
// }));
// world.addLayer(new OpenEarthView.Layer.OSM(
// "OpenCycleMap", [
// "http://c.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png"
// ]));
world.addLayer(new OpenEarthView.Layer.OSM(
"OpenCycleMap", [
"http://c.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png"
]));
"GoogleMap", [
"http://mt0.google.com/vt/lyrs=y&x=${x}&y=${y}&z=${z}",
"http://mt1.google.com/vt/lyrs=y&x=${x}&y=${y}&z=${z}",
"http://mt2.google.com/vt/lyrs=y&x=${x}&y=${y}&z=${z}",
"http://mt3.google.com/vt/lyrs=y&x=${x}&y=${y}&z=${z}"
], {
mapTextureRequest: 10
}));
// world.addLayer(new OpenEarthView.Layer.OSM(
// "OpenStreetMap", [
// "https://a.tile.openstreetmap.org/${z}/${x}/${y}.png",
// "https://b.tile.openstreetmap.org/${z}/${x}/${y}.png",
// "https://c.tile.openstreetmap.org/${z}/${x}/${y}.png"
// ]));
// world.addLayer(new OpenEarthView.Layer.Elevation(
// "OpenEarthView", [
// 'http://localhost:8084/oevElevation?tile=${z},${x},${y},${lod}'
// ]));
// world.addTerrain(new OpenEarthView.Terrain.FlatTerrain("FlatTerrain"));
// UNESCO
// world.setCenter(2.3057599523656336, 48.849568465379264);
// LOUVRE
// world.setCenter(2.3364709900972684, 48.86045538042749);
world.setCenter(2.3364709900972684, 48.86045538042749);
// MANHATTAN
world.setCenter(286.02073690310226, 40.747508647167834);
// world.setCenter(286.02073690310226, 40.747508647167834);
</script>
</body>
......
......@@ -5,8 +5,12 @@
// "http://c.tile.openstreetmap.org/${z}/${x}/${y}.png"
// ]));
OpenEarthView.Layer.Building = function(name, urls) {
OpenEarthView.Layer.Building = function(name, urls, options) {
this.name = (name !== undefined) ? name : 'OpenEarthView';
if (options !== undefined) {
this.minZoom = (options.minZoom !== undefined)?options.minZoom:18;
console.log('minZoom: ' + this.minZoom);
}
if (OpenEarthViewLayers.hasOwnProperty(name)) {
console.err('Cannot register this already existing layer !');
return;
......@@ -22,7 +26,7 @@ OpenEarthView.Layer.Building.prototype = {
getName: function() {
return this.name
},
getUrl: function(zoom, xtile, ytile, factor) {
getUrl: function(zoom, xtile, ytile) {
var scope = this;
var urls = OpenEarthViewLayers[scope.name];
var urlRandom = urls[
......@@ -34,8 +38,8 @@ OpenEarthView.Layer.Building.prototype = {
// // console.log("Match!");
// return p1 + zoom + p2 + xtile + p3 + ytile + p4;
// });
var url = urlRandom.replace('${f}', factor);
var url = url.replace('${z}', zoom);
// var url = urlRandom.replace('${f}', factor);
var url = urlRandom.replace('${z}', zoom);
var url = url.replace('${x}', xtile);
return url.replace('${y}', ytile);
// return url;
......
// world.addLayer(new OpenEarthView.Layer.OSM(
// "OpenStreetMap", [
// "http://a.tile.openstreetmap.org/${z}/${x}/${y}.png",
// "http://b.tile.openstreetmap.org/${z}/${x}/${y}.png",
// "http://c.tile.openstreetmap.org/${z}/${x}/${y}.png"
// ]));
var toolbox = OpenEarthView.toolbox;
OpenEarthView.Layer.OverpassBuilding = function(name, urls, options) {
this.name = (name !== undefined) ? name : 'Overpass';
this.minZoom = 17;
if (options !== undefined) {
this.minZoom = (options.minZoom !== undefined) ? options.minZoom : 18;
// console.log('minZoom: ' + this.minZoom);
}
if (OpenEarthViewLayers.hasOwnProperty(name)) {
console.err('Cannot register this already existing layer !');
return;
}
OpenEarthViewLayers[name] = (urls !== undefined) ? urls : [
'http://overpass-api.de/api/interpreter'
];
};
OpenEarthView.Layer.OverpassBuilding.prototype = {
constructor: OpenEarthView.Layer.Building,
type: 'overpassBuilding',
getName: function() {
return this.name
},
getUrl: function(zoom, xtile, ytile) {
var scope = this;
var urls = OpenEarthViewLayers[scope.name];
var urlRandom = urls[
Math.floor(Math.random() * urls.length)];
// Process GPS bounds
minLon = toolbox.tile2long(xtile, zoom);
maxLon = toolbox.tile2long(xtile + 1, zoom);
minLat = toolbox.tile2lat(ytile + 1, zoom);
maxLat = toolbox.tile2lat(ytile, zoom);
var url = urlRandom;
url = url + '?data=[out:json];' +
'((relation["building"](' + minLat + ',' + minLon + ',' + maxLat + ',' + maxLon + ');>;);' +
'(way["building"](' + minLat + ',' + minLon + ',' + maxLat + ',' + maxLon + ');>;););' +
'out center;'
return url;
}
}
......@@ -24,10 +24,13 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* @author Clement Igonet
*/
THREE.GeojsonLoader = function(manager) {
var singleton = undefined;
THREE.GeojsonLoader = function(manager) {
this.manager = (manager !== undefined) ? manager : THREE.DefaultLoadingManager;
if (singleton === undefined) {
singleton = this;
}
};
var R = 6378.137;
......@@ -37,24 +40,48 @@ var geojsonRequests = {};
var geojsonAliveRequests = {};
var geojsonAliveRequestsCount = 0;
var geojsonRequestsCount = 0;
var MAX_GEOJSON_REQUEST = 1;
var MAX_GEOJSON_REQUEST = 10;
// THREE.GeojsonLoader.getSingleton = function(manager) {}
// scope = GeojsonLoader.getSingleton();
THREE.GeojsonLoader.getSingleton = function() {
if (singleton === undefined) {
singleton = new THREE.GeojsonLoader();
}
return singleton;
}
THREE.GeojsonLoader.prototype = {
constructor: THREE.GeojsonLoader,
crossOrigin: undefined,
load: function(url, onLoad, onProgress, onError, lod, defaultColor) {
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
// console.log('Loading (0): ', tilePath);
// console.log('Loading: ', url);
var scope = this;
var loader = new THREE.XHRLoader(scope.manager);
if (geojsons.hasOwnProperty(url)) {
onLoad(scope.parse(JSON.parse(geojsons[url]), lod, defaultColor));
if (geojsons.hasOwnProperty(tilePath)) {
// onLoad(scope.parse(JSON.parse(geojsons[url]), lod, defaultColor));
geojson = geojsons[tilePath];
try {
// console.log('Loading (1): ', tilePath);
var myGeojson = JSON.parse(geojson);
onLoad(scope.parse(myGeojson, lod, defaultColor));
} catch (e) {
console.error('Cannot parse geojson data (' + tilePath + ')');
console.error('Error message: ', e.message);
onLoad(scope.parse({}, lod, defaultColor));
}
} else {
geojsonRequestsCount = geojsonRequestsCount +
(geojsonRequests.hasOwnProperty(url) ? 0 : 1);
geojsonRequests[url] = {
(geojsonRequests.hasOwnProperty(tilePath) ? 0 : 1);
geojsonRequests[tilePath] = {
url: url,
onLoad: onLoad,
onProgress: onProgress,
onError: onError,
......@@ -74,38 +101,62 @@ THREE.GeojsonLoader.prototype = {
var scope = this;
var loader = new THREE.XHRLoader(scope.manager);
while (geojsonAliveRequestsCount < MAX_GEOJSON_REQUEST && geojsonRequestsCount > 0) {
var urls = Object.keys(geojsonRequests);
var url = urls[urls.length - 1];
var tilePaths = Object.keys(geojsonRequests);
var tilePath = tilePaths[tilePaths.length - 1];
geojsonAliveRequestsCount = geojsonAliveRequestsCount +
(geojsonAliveRequests.hasOwnProperty(url) ? 0 : 1);
console.log('geojsonAliveRequestsCount:', geojsonAliveRequestsCount);
geojsonAliveRequests[url] = geojsonRequests[url];
var onLoad = geojsonAliveRequests[url].onLoad;
var onProgress = geojsonAliveRequests[url].onProgress;
var onError = geojsonAliveRequests[url].onError;
var lod = geojsonAliveRequests[url].lod;
var defaultColor = geojsonAliveRequests[url].defaultColor;
delete geojsonRequests[url];
(geojsonAliveRequests.hasOwnProperty(tilePath) ? 0 : 1);
// if (geojsonAliveRequestsCount === MAX_GEOJSON_REQUEST)
// console.log('geojsonAliveRequestsCount:', geojsonAliveRequestsCount);
geojsonAliveRequests[tilePath] = geojsonRequests[tilePath];
var url = geojsonAliveRequests[tilePath].url;
var onLoad = geojsonAliveRequests[tilePath].onLoad;
var onProgress = geojsonAliveRequests[tilePath].onProgress;
var onError = geojsonAliveRequests[tilePath].onError;
var lod = geojsonAliveRequests[tilePath].lod;
var defaultColor = geojsonAliveRequests[tilePath].defaultColor;
delete geojsonRequests[tilePath];
geojsonRequestsCount--;
(function(url, onLoad, onProgress, onError, lod, defaultColor) {
// console.log('Loading (3): ', tilePath);
loader.load(
url,
function(geojson) {
function(geojsonTile) {
// console.log('geojson:', geojson);
geojsons[url] = geojson;
if (geojsonAliveRequests.hasOwnProperty(url)) {
delete geojsonAliveRequests[url];
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
if (geojsonAliveRequests.hasOwnProperty(tilePath)) {
delete geojsonAliveRequests[tilePath];
geojsonAliveRequestsCount--;
console.log('geojsonAliveRequestsCount:', geojsonAliveRequestsCount);
onLoad(scope.parse(JSON.parse(geojson), lod, defaultColor));
// if (geojsonAliveRequestsCount === 0)
// console.log('geojsonAliveRequestsCount back to 0:', geojsonAliveRequestsCount);
try {
var myGeojson = JSON.parse(geojsonTile);
// for (var tilePath in geojsonTiles.getKey()) {
geojsons[tilePath] = geojsonTile;
// }
// geojsons[tilePath] = geojson;
// var xxx = THREE.GeojsonLoader.getSingleton();
onLoad(THREE.GeojsonLoader.getSingleton().parse(myGeojson, lod, defaultColor));
// console.error('Cannot parse geojson data (' + tilePath + ')');
// console.error('Error message: ', e.message);
} catch (e) {
console.error('Error when parsing geojson data (' + tilePath + '): ', geojsonTile);
console.error(e);
onLoad(scope.parse({}, lod, defaultColor));
}
// onLoad(scope.parse(JSON.parse(geojson), lod, defaultColor));
}
scope.loadNextGeojson();
}, onProgress,
function(geojson) {
if (geojsonAliveRequests.hasOwnProperty(url)) {
delete geojsonAliveRequests[url];
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
if (geojsonAliveRequests.hasOwnProperty(tilePath)) {
delete geojsonAliveRequests[tilePath];
geojsonAliveRequestsCount--;
console.log('geojsonAliveRequestsCount:', geojsonAliveRequestsCount);
// if (geojsonAliveRequestsCount === 0)
// console.log('geojsonAliveRequestsCount back to 0:', geojsonAliveRequestsCount);
}
scope.loadNextGeojson();
});
......@@ -116,6 +167,7 @@ THREE.GeojsonLoader.prototype = {
this.crossOrigin = value;
},
roofMesh: function(feature) {
var scope = this;
var measure = OpenEarthView.toolbox.measure;
// var roofMesh = new THREE.Mesh(roofGeometry, roofMaterial);
var prop = feature.properties;
......@@ -176,7 +228,8 @@ THREE.GeojsonLoader.prototype = {
transparent: false,
opacity: 0.4
});
if (lod == 5) {
// console.log('lod:', lod)
if (lod == 4) {
roofMaterial.transparent = true;
}
......@@ -190,21 +243,20 @@ THREE.GeojsonLoader.prototype = {
return roofMesh;
},
parse: function(json, lod, defaultColor) {
var measure = OpenEarthView.toolbox.measure;
var scope = this;
var measure = OpenEarthView.toolbox.measure;
var lonOri, latOri;
var tile = new THREE.Object3D();
var geojsons;
var geojsonArray;
if (json.constructor === Array) {
geojsons = json;
geojsonArray = json;
} else {
geojsons = [json];
geojsonArray = [json];
}
var lonOri, latOri;
for (var geoIdx = 0; geoIdx < geojsons.length; geoIdx++) {
var geojson = geojsons[geoIdx];
// console.log('geojson:', JSON.stringify(geojson));
for (var geoIdx = 0; geoIdx < geojsonArray.length; geoIdx++) {
var geojson = geojsonArray[geoIdx];
switch (geojson.type) {
case 'Feature':
switch (geojson.properties.type) {
......@@ -230,8 +282,8 @@ THREE.GeojsonLoader.prototype = {
// var geometry = new THREE.ShapeGeometry(rectShape);
var material = new THREE.LineBasicMaterial({
color: 0xff0000,
linewidth: 3
color: defaultColor,
linewidth: 10
});
var mesh = new THREE.Line(points, material);
mesh.position.z = 1;
......@@ -311,7 +363,7 @@ THREE.GeojsonLoader.prototype = {
transparent: false,
opacity: 0.4
});
if (lod == 5) {
if (lod == 4) {
material.transparent = true;
}
......
/**
Open Earth View - viewer-threejs
The MIT License (MIT)
Copyright (c) 2016 Clément Igonet
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* @author Clement Igonet
*/
var singleton = undefined;
THREE.OverpassJsonLoader = function(manager) {
this.manager = (manager !== undefined) ? manager : THREE.DefaultLoadingManager;
if (singleton === undefined) {
singleton = this;
}
};
var R = 6378.137;
var overpassJsons = {};
var overpassJsonRequests = {};
var overpassJsonAliveRequests = {};
var overpassJsonAliveRequestsCount = 0;
var overpassJsonRequestsCount = 0;
var MAX_OVERPASS_JSON_REQUEST = 10;
// THREE.OverpassJsonLoader.getSingleton = function(manager) {}
// scope = OverpassJsonLoader.getSingleton();
THREE.OverpassJsonLoader.getSingleton = function() {
if (singleton === undefined) {
singleton = new THREE.OverpassJsonLoader();
}
return singleton;
}
THREE.OverpassJsonLoader.prototype = {
constructor: THREE.OverpassJsonLoader,
crossOrigin: undefined,
load: function(myTile, url, onLoad, onProgress, onError, lod, defaultColor) {
// console.log("in - THREE.OverpassJsonLoader.load");
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
// console.log('Loading (0): ', tilePath);
// console.log('Loading: ', url);
var scope = this;
var loader = new THREE.XHRLoader(scope.manager);
if (overpassJsons.hasOwnProperty(tilePath)) {
// onLoad(scope.parse(JSON.parse(overpassJsons[url]), lod, defaultColor));
overpassJson = overpassJsons[tilePath];
try {
// console.log('Loading (1): ', tilePath);
var myOverpassJson = JSON.parse(overpassJson);
onLoad(scope.parse(myOverpassJson, myTile, lod, defaultColor));
// parse: function(json, bounds, lod, defaultColor) {
} catch (e) {
console.error('Cannot parse overpassJson data (' + tilePath + ')');
console.error('Error message: ', e.message);
onLoad(scope.parse({}, myTile, lod, defaultColor));
}
} else {
overpassJsonRequestsCount = overpassJsonRequestsCount +
(overpassJsonRequests.hasOwnProperty(tilePath) ? 0 : 1);
overpassJsonRequests[tilePath] = {
myTile: myTile,
url: url,
onLoad: onLoad,
onProgress: onProgress,
onError: onError,
lod: lod,
defaultColor: defaultColor
}
scope.loadNextOverpassJson();
}
// loader.load(url, function(text) {
// // console.log('defaultColor:', defaultColor);
// onLoad(scope.parse(JSON.parse(text), defaultColor));
// // console.log('Loaded: ', url);
// }, onProgress, onError);
},
loadNextOverpassJson: function() {
var scope = this;
var loader = new THREE.XHRLoader(scope.manager);
while (overpassJsonAliveRequestsCount < MAX_OVERPASS_JSON_REQUEST && overpassJsonRequestsCount > 0) {
var tilePaths = Object.keys(overpassJsonRequests);
var tilePath = tilePaths[tilePaths.length - 1];
overpassJsonAliveRequestsCount = overpassJsonAliveRequestsCount +
(overpassJsonAliveRequests.hasOwnProperty(tilePath) ? 0 : 1);
// if (overpassJsonAliveRequestsCount === MAX_GEOJSON_REQUEST)
// console.log('overpassJsonAliveRequestsCount:', overpassJsonAliveRequestsCount);
overpassJsonAliveRequests[tilePath] = overpassJsonRequests[tilePath];
var myTile = overpassJsonAliveRequests[tilePath].myTile;
var url = overpassJsonAliveRequests[tilePath].url;
var onLoad = overpassJsonAliveRequests[tilePath].onLoad;
var onProgress = overpassJsonAliveRequests[tilePath].onProgress;
var onError = overpassJsonAliveRequests[tilePath].onError;
var lod = overpassJsonAliveRequests[tilePath].lod;
var defaultColor = overpassJsonAliveRequests[tilePath].defaultColor;
delete overpassJsonRequests[tilePath];
overpassJsonRequestsCount--;
(function(myTile, url, onLoad, onProgress, onError, lod, defaultColor) {
// console.log('function - myTile:', myTile);
// console.log('Loading (3): ', tilePath);
loader.load(
url,
function(overpassJsonTile) {
// console.log('overpassJsonTile:', overpassJsonTile);
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
console.log('tilePath:', tilePath);
if (overpassJsonAliveRequests.hasOwnProperty(tilePath)) {
delete overpassJsonAliveRequests[tilePath];
overpassJsonAliveRequestsCount--;
// if (overpassJsonAliveRequestsCount === 0)
// console.log('overpassJsonAliveRequestsCount back to 0:', overpassJsonAliveRequestsCount);
try {
var myOverpassJson = JSON.parse(overpassJsonTile);
// for (var tilePath in overpassJsonTiles.getKey()) {
overpassJsons[tilePath] = overpassJsonTile;
// }
// overpassJsons[tilePath] = overpassJson;
// var xxx = THREE.OverpassJsonLoader.getSingleton();
onLoad(THREE.OverpassJsonLoader.getSingleton().parse(myOverpassJson, myTile, lod, defaultColor));
// console.error('Cannot parse overpassJson data (' + tilePath + ')');
// console.error('Error message: ', e.message);
} catch (e) {
console.error('Error when parsing overpassJson data (' + tilePath + '): ', overpassJsonTile);
// Error when parsing overpassJson data (/api/interpreter?data=[out:json];((relation[%22building%22](48.86064927570691,2.3345947265625,48.86110101269274,2.3352813720703125);%3E;);(way[%22building%22](48.86064927570691,2.3345947265625,48.86110101269274,2.3352813720703125);%3E;););outenter;):
console.error(e);
onLoad(scope.parse({}, myTile, lod, defaultColor));
}
// onLoad(scope.parse(JSON.parse(overpassJson), lod, defaultColor));
}
scope.loadNextOverpassJson();
}, onProgress,
function(overpassJson) {
var myUrl = new URL(url);
var tilePath = myUrl.pathname + myUrl.search;
if (overpassJsonAliveRequests.hasOwnProperty(tilePath)) {
delete overpassJsonAliveRequests[tilePath];
overpassJsonAliveRequestsCount--;
// if (overpassJsonAliveRequestsCount === 0)
// console.log('overpassJsonAliveRequestsCount back to 0:', overpassJsonAliveRequestsCount);
}
scope.loadNextOverpassJson();
});
})(myTile, url, onLoad, onProgress, onError, lod, defaultColor);
}
},
setCrossOrigin: function(value) {
this.crossOrigin = value;
},
parse: function(json, myTile, lod, defaultColor) {
// console.log('json:', JSON.stringify(json));
var scope = this;
var measure = OpenEarthView.toolbox.measure;
var tile2long = OpenEarthView.toolbox.tile2long;
var tile2lat = OpenEarthView.toolbox.tile2lat;
var lonOri, latOri;
var tile = new THREE.Object3D();
var overpassJson = json;
var bounds = {
minLon: tile2long(myTile.x, myTile.z),
maxLon: tile2long(myTile.x + 1, myTile.z),
minLat: tile2lat(myTile.y + 1, myTile.z),
maxLat: tile2lat(myTile.y, myTile.z)
}
console.log('bounds:', JSON.stringify(bounds));
var lonOri = bounds.minLon;
var latOri = bounds.maxLat;
var nodes = {};
// TODO: deal with empty data ( {} )
var elements = (overpassJson.hasOwnProperty('elements')) ? overpassJson.elements : [];
for (var eltIdx = 0; eltIdx < elements.length; eltIdx++) {
var element = elements[eltIdx];
switch (element.type) {
case 'node':
nodes[element.id] = element;
break;
case 'way':
var way = element;
// console.log('way:', JSON.stringify(way));
// console.log('tags:', JSON.stringify(way.tags));
if (way.tags.hasOwnProperty("building")) {
console.log('isBuilding !');
var minHeight = (way.tags.hasOwnProperty('minHeight')) ?
way.tags.minHeight : 0;
var roofHeight = (way.tags.hasOwnProperty('roof:height')) ?
way.tags['roof:height'] : 0;
var height = (way.tags.hasOwnProperty('height') && way.tags.height != null) ?
(way.tags.height - roofHeight) : 20;
var color = (way.tags.hasOwnProperty('color')) ?
way.tags.color : defaultColor;
if (way.nodes.length <= 2) {
break;
}
if (bounds.minLon > way.center.lon || way.center.lon > bounds.maxLon ||
bounds.minLat > way.center.lat || way.center.lat > bounds.maxLat) {
break;
}
var shapePts = [];
// console.log('way.nodes:', way.nodes);
for (var nodeIdx = 0; nodeIdx < way.nodes.length; nodeIdx++) {
// console.log('way.nodes[', nodeIdx, ']:', way.nodes[nodeIdx]);
// console.log('nodes[', way.nodes[nodeIdx], ':', nodes[way.nodes[nodeIdx]]);
var lon = nodes[way.nodes[nodeIdx]].lon;
var lat = nodes[way.nodes[nodeIdx]].lat;
console.log('lon:', lon);
console.log('lat:', lat);
console.log('lonOri:', lonOri);
console.log('latOri:', latOri);
var x = ((lon - lonOri) / Math.abs(lonOri - lon)) * measure(latOri, lonOri, latOri, lon);
var y = ((lat - latOri) / Math.abs(latOri - lat)) * measure(latOri, lonOri, lat, lonOri);
console.log('x:', x);
console.log('y:', y);
shapePts.push(new THREE.Vector2(x, y));
}
// if (shapePts.length > 0) console.log(JSON.stringify(shapePts));
var shape = new THREE.Shape(shapePts);
var extrudeSettings = {
amount: height,
bevelEnabled: false,
bevelSegments: 1,
steps: 1,
bevelSize: 2,
bevelThickness: 1
};
var geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
var material = new THREE.MeshPhongMaterial({
color: color,
transparent: false,
opacity: 0.4
});
if (lod == 4) {
material.transparent = true;