Skip to content
Permalink
Browse files

enabled adding OSMBuildingLayer using local GeoJSON

  • Loading branch information...
kilsedar committed Jul 14, 2017
1 parent 4e91b57 commit 269af231af5c04c70de1f6fdec5da28088040333
Showing with 195 additions and 25 deletions.
  1. +137 −0 example/data/test.geojson
  2. +14 −14 example/index.js
  3. +39 −7 src/OSMBuildingLayer.js
  4. +3 −2 src/OSMLayer.js
  5. +2 −2 src/shapes/BuildingShape.js
@@ -0,0 +1,137 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.0692138671875,
44.654611643129996
],
[
9.079341888427733,
44.654611643129996
],
[
9.079341888427733,
44.6587633171262
],
[
9.0692138671875,
44.6587633171262
],
[
9.0692138671875,
44.654611643129996
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.079341888427733,
44.64594101249175
],
[
9.09170150756836,
44.64594101249175
],
[
9.09170150756836,
44.646795920997846
],
[
9.079341888427733,
44.646795920997846
],
[
9.079341888427733,
44.64594101249175
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.088268280029297,
44.653512620845966
],
[
9.095478057861328,
44.653512620845966
],
[
9.095478057861328,
44.659129627031355
],
[
9.088268280029297,
44.659129627031355
],
[
9.088268280029297,
44.653512620845966
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.081401824951172,
44.67097240018886
],
[
9.083118438720703,
44.66779828595748
],
[
9.080543518066406,
44.664990270779555
],
[
9.089813232421875,
44.665966987139186
],
[
9.092216491699219,
44.67085032208647
],
[
9.089298248291016,
44.67548910920999
],
[
9.081401824951172,
44.67097240018886
]
]
]
}
}
]
}
@@ -33,18 +33,18 @@ define(['libraries/WebWorldWind/src/WorldWind', 'src/OSMBuildingLayer'],
altitude: "osm",
altitudeMode: WorldWind.RELATIVE_TO_GROUND
};
// var osmMilan = new OSMBuildingLayer(worldWindow, [45.48, 9.45, 45.50, 9.50], configuration);
// var osmMilan = new OSMBuildingLayer(worldWindow, [45.45, 9.05, 45.5, 9.1], configuration);
// var osmMilan = new OSMBuildingLayer(worldWindow, [45.3871, 9.04284, 45.536, 9.27791], configuration);
// var osmMilan = new OSMBuildingLayer(worldWindow, [45.48, 9.2, 45.49, 9.21], configuration); // buggy region (nodes)
// var osmMilan = new OSMBuildingLayer(worldWindow, [45.4557, 9.1705, 45.4735, 9.2021], configuration); // center
/* osmMilan.add();
osmMilan.zoom(); */
// var osmNewYork = new OSMBuildingLayer(worldWindow, [40.6998, -74.0232, 40.74, -73.97], configuration);
var osmNewYork = new OSMBuildingLayer(worldWindow, configuration);
osmNewYork.addByBoundingBox([40.70, -74.03, 40.72, -74.0]);
osmNewYork.zoom();
/* var osmMilan = new OSMBuildingLayer(worldWindow, [45.18, 9.48, 45.19, 9.53], configuration);
osmMilan.add();
osmMilan.zoom(); */

// var osmNewYork = new OSMBuildingLayer(worldWindow, configuration);
// osmNewYork.addByBoundingBox([-74.0232, 40.6998, -73.97, 40.74]);
// osmNewYork.addByBoundingBox([-74.03, 40.70, -74.0, 40.72]);
// var osmMilan = new OSMBuildingLayer(worldWindow, configuration);
// osmMilan.addByBoundingBox([9.45, 45.48, 9.50, 45.50]);
// osmMilan.addByBoundingBox([9.05, 45.45, 9.10, 45.50]);
// osmMilan.addByBoundingBox([9.04284, 45.3871, 9.27791, 45.536]);
// osmMilan.addByBoundingBox([9.2, 45.48, 9.21, 45.49]); // buggy region (nodes)
// osmMilan.addByBoundingBox([9.1705, 45.4557, 9.2021, 45.4735]); // center
// osmMilan.addByBoundingBox([9.48, 45.18, 9.53, 45.19]); // region tested in GRASS
// osmMilan.zoom();
var test = new OSMBuildingLayer(worldWindow, configuration);
test.addByGeoJSONFile("data/test.geojson");
});
@@ -107,9 +107,9 @@ define(['libraries/WebWorldWind/src/cache/MemoryCache',
var _self = this;

var data = '[out:json][timeout:25];';
data += '(' + this._type + '[' + this._tag + '](' + boundingBox[0] + ',' + boundingBox[1] + ',' + boundingBox[2] + ',' + boundingBox[3] + '); ';
// data += 'relation[' + this._tag + '](' + boundingBox[0] + ',' + boundingBox[1] + ',' + boundingBox[2] + ',' + boundingBox[3] + ');); (._;>;); out body qt;';
data += 'relation[' + this._tag + '](' + boundingBox[0] + ',' + boundingBox[1] + ',' + boundingBox[2] + ',' + boundingBox[3] + ');); out body; >; out skel qt;';
data += '(' + this._type + '[' + this._tag + '](' + boundingBox[1] + ',' + boundingBox[0] + ',' + boundingBox[3] + ',' + boundingBox[2] + '); ';
// data += 'relation[' + this._tag + '](' + boundingBox[1] + ',' + boundingBox[0] + ',' + boundingBox[3] + ',' + boundingBox[2] + ');); (._;>;); out body qt;';
data += 'relation[' + this._tag + '](' + boundingBox[1] + ',' + boundingBox[0] + ',' + boundingBox[3] + ',' + boundingBox[2] + ');); out body; >; out skel qt;';
// console.log("data --> " + data);

$.ajax({
@@ -124,7 +124,7 @@ define(['libraries/WebWorldWind/src/cache/MemoryCache',
var dataOverpassGeoJSON = osmtogeojson(dataOverpass);
_self.cache(dataOverpassGeoJSON);
var dataOverpassGeoJSONString = JSON.stringify(dataOverpassGeoJSON);
// console.log("dataOverpassGeoJSONString --> " + dataOverpassGeoJSONString);
console.log("dataOverpassGeoJSONString --> " + dataOverpassGeoJSONString);
// console.log("dataOverpassGeoJSON.features.length (number of polygons) --> " + dataOverpassGeoJSON.features.length);
// console.time("creatingOSMBuildingLayer");
var OSMBuildingLayer = new WorldWind.RenderableLayer("OSMBuildingLayer");
@@ -133,6 +133,7 @@ define(['libraries/WebWorldWind/src/cache/MemoryCache',
OSMBuildingLayerGeoJSON.load(null, _self.shapeConfigurationCallback.bind(_self), OSMBuildingLayer);
// console.timeEnd("creatingOSMBuildingLayer");
worldWindow.addLayer(OSMBuildingLayer);
_self.zoom();
},
error: function (e) {
console.log("Error: " + JSON.stringify(e));
@@ -142,16 +143,47 @@ define(['libraries/WebWorldWind/src/cache/MemoryCache',

/**
*
* @param
*/
OSMBuildingLayer.prototype.addByFile = function (path) {

OSMBuildingLayer.prototype.calculateBoundingBox = function (GeoJSON) {
var boundingBox = [Infinity, Infinity, -Infinity, -Infinity], polygons, coordinates, latitude, longitude;

for (var featureIndex = 0; featureIndex < GeoJSON.features.length; featureIndex++) {
polygons = GeoJSON.features[featureIndex].geometry.coordinates;

for (var polygonsIndex = 0; polygonsIndex < polygons.length; polygonsIndex++) {
for (var coordinatesIndex = 0; coordinatesIndex < polygons[polygonsIndex].length; coordinatesIndex++) {
longitude = polygons[polygonsIndex][coordinatesIndex][0];
latitude = polygons[polygonsIndex][coordinatesIndex][1];
// console.log(longitude + ", " + latitude);
boundingBox[0] = boundingBox[0] < longitude ? boundingBox[0] : longitude; // minimum longitude (x1)
boundingBox[1] = boundingBox[1] < latitude ? boundingBox[1] : latitude; // minimum latitude (y1)
boundingBox[2] = boundingBox[2] > longitude ? boundingBox[2] : longitude; // maximum longitude (x2)
boundingBox[3] = boundingBox[3] > latitude ? boundingBox[3] : latitude; // maximum latitude (y2)
}
}
}
this.boundingBox = boundingBox;
// console.log(this.boundingBox);
};

/**
*
* @param
*/
OSMBuildingLayer.prototype.addByURL = function (path) {
OSMBuildingLayer.prototype.addByGeoJSONFile = function (path) {
var worldWindow = this.worldWindow;
var _self = this;

$.getJSON(path, function(data) {
_self.calculateBoundingBox(data);
var GeoJSONString = JSON.stringify(data);
var OSMBuildingLayer = new WorldWind.RenderableLayer("OSMBuildingLayer");
var OSMBuildingLayerGeoJSON = new GeoJSONParserTriangulationOSM(GeoJSONString);
OSMBuildingLayerGeoJSON.load(null, _self.shapeConfigurationCallback.bind(_self), OSMBuildingLayer);
worldWindow.addLayer(OSMBuildingLayer);
_self.zoom();
})
};

return OSMBuildingLayer;
@@ -116,15 +116,16 @@ define(['libraries/WebWorldWind/src/error/ArgumentError',
/**
* Zooms to the OSMLayer, by setting the center of the viewport to the center of the bounding box.
* It uses an arbitrary value for the range of {@link LookAtNavigator}.
* To be removed later.
* @throws {ArgumentError} If boundingBox of the layer is null.
*/
OSMLayer.prototype.zoom = function () {
if (this.boundingBox != null) {
var boundingBox = this.boundingBox;
var centerX = (boundingBox[0] + boundingBox[2])/2;
var centerY = (boundingBox[1] + boundingBox[3])/2;
this.worldWindow.navigator.lookAtLocation.latitude = centerX;
this.worldWindow.navigator.lookAtLocation.longitude = centerY;
this.worldWindow.navigator.lookAtLocation.longitude = centerX;
this.worldWindow.navigator.lookAtLocation.latitude = centerY;
// console.log(centerX + ", " + centerY);
this.worldWindow.navigator.range = 1e4; // Should be automatically calculated.
this.worldWindow.redraw();
@@ -98,9 +98,9 @@ define([''], function () {
BuildingShape.prototype.setAltitude = function (configuration) {
var altitude;
if (configuration.extrude && configuration.altitude == "osm") {
if (this.properties.tags.height)
if (this.properties.tags && this.properties.tags.height)
altitude = this.properties.tags.height;
else if (this.properties.tags["building:levels"])
else if (this.properties.tags && this.properties.tags["building:levels"])
altitude = this.properties.tags["building:levels"]*3;
else
altitude = 15;

0 comments on commit 269af23

Please sign in to comment.
You can’t perform that action at this time.