diff --git a/example/index.js b/example/index.js index 8fe1b89..0ff9458 100755 --- a/example/index.js +++ b/example/index.js @@ -41,7 +41,10 @@ define(['libraries/WebWorldWind/src/WorldWind', 'src/OSMBuildingLayer'], /* osmMilan.add(); osmMilan.zoom(); */ // var osmNewYork = new OSMBuildingLayer(worldWindow, [40.6998, -74.0232, 40.74, -73.97], configuration); - var osmNewYork = new OSMBuildingLayer(worldWindow, [40.70, -74.03, 40.72, -74.0], configuration); - osmNewYork.add(); + 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(); */ }); diff --git a/src/GeoJSONParserTriangulation.js b/src/GeoJSONParserTriangulation.js index 8dcc1b1..d490145 100644 --- a/src/GeoJSONParserTriangulation.js +++ b/src/GeoJSONParserTriangulation.js @@ -1,13 +1,12 @@ /** * @exports GeoJSONParserTriangulation */ -define(['libraries/WebWorldWind/src/WorldWind', - 'libraries/WebWorldWind/src/formats/geojson/GeoJSONParser', +define(['libraries/WebWorldWind/src/formats/geojson/GeoJSONParser', 'libraries/WebWorldWind/src/geom/Position', 'libraries/WebWorldWind/src/shapes/TriangleMesh', 'src/shapes/BuildingShape', 'earcut'], - function (WorldWind, GeoJSONParser, Position, TriangleMesh, BuildingShape, earcut) { + function (GeoJSONParser, Position, TriangleMesh, BuildingShape, earcut) { "use strict"; /** diff --git a/src/GeoJSONParserTriangulationOSM.js b/src/GeoJSONParserTriangulationOSM.js index 3c71d55..d805c8c 100644 --- a/src/GeoJSONParserTriangulationOSM.js +++ b/src/GeoJSONParserTriangulationOSM.js @@ -1,13 +1,9 @@ /** * @exports GeoJSONParserTriangulationOSM */ -define(['libraries/WebWorldWind/src/WorldWind', - 'libraries/WebWorldWind/src/geom/Position', - 'libraries/WebWorldWind/src/shapes/TriangleMesh', - 'src/GeoJSONParserTriangulation', - 'src/shapes/BuildingShape', - 'earcut'], - function (WorldWind, Position, TriangleMesh, GeoJSONParserTriangulation, BuildingShape, earcut) { +define(['src/GeoJSONParserTriangulation', + 'src/shapes/BuildingShape'], + function (GeoJSONParserTriangulation, BuildingShape) { "use strict"; /** diff --git a/src/OSMBuildingLayer.js b/src/OSMBuildingLayer.js index c89aa36..67f6dd2 100644 --- a/src/OSMBuildingLayer.js +++ b/src/OSMBuildingLayer.js @@ -1,13 +1,12 @@ /** * @exports OSMBuildingLayer */ -define(['libraries/WebWorldWind/src/WorldWind', - 'libraries/WebWorldWind/src/cache/MemoryCache', +define(['libraries/WebWorldWind/src/cache/MemoryCache', 'src/OSMLayer', 'src/GeoJSONParserTriangulationOSM', 'jquery', 'osmtogeojson'], - function (WorldWind, MemoryCache, OSMLayer, GeoJSONParserTriangulationOSM, $, osmtogeojson) { + function (MemoryCache, OSMLayer, GeoJSONParserTriangulationOSM, $, osmtogeojson) { "use strict"; /** @@ -16,11 +15,10 @@ define(['libraries/WebWorldWind/src/WorldWind', * @constructor * @classdesc Fetches OSM buildings, converts them to GeoJSON, and adds them to the WorldWindow. * @param {WorldWindow} worldWindow The WorldWindow where the OSMBuildingLayer is added to. - * @param {Float[]} boundingBox It defines the bounding box of the OSM data for the OSMLayer. The order of coordinates of the bounding box is "x1, y1, x2, y2". * @param {Object} configuration Configuration is used to set the attributes of {@link ShapeAttributes}. Four more attributes can be defined, which are "extrude", "heatmap", "altitude" and "altitudeMode". */ - var OSMBuildingLayer = function (worldWindow, boundingBox, configuration) { - OSMLayer.call(this, worldWindow, boundingBox, configuration); + var OSMBuildingLayer = function (worldWindow, configuration) { + OSMLayer.call(this, worldWindow, configuration); this.type = "way"; this.tag = "building"; this._geometryCache = new MemoryCache(30000, 24000); @@ -95,13 +93,17 @@ define(['libraries/WebWorldWind/src/WorldWind', }; /** - * Using the boundingBox of the OSMBuildingLayer, fetches the OSM building data using Overpass API, converts it to GeoJSON using osmtogeojson API, - * adds the GeoJSON data to the WorldWindow using the {@link GeoJSONParserTriangulationOSM}. + * Using the boundingBox fetches the OSM building data using Overpass API, converts it to GeoJSON using osmtogeojson API, + * adds the GeoJSON to the WorldWindow using the {@link GeoJSONParserTriangulationOSM}. + * It also sets the boundingBox of the {@link OSMLayer}. + * @param {Float[]} boundingBox It defines the bounding box of the OSM data to add. */ - OSMBuildingLayer.prototype.add = function () { + OSMBuildingLayer.prototype.addByBoundingBox = function (boundingBox) { - var boundingBox = this.boundingBox; + this.boundingBox = boundingBox; var worldWindow = this.worldWindow; + /* var dc = this.worldWindow.drawContext; + console.log("dc.globe --> " + dc.globe); */ var _self = this; var data = '[out:json][timeout:25];'; @@ -138,5 +140,19 @@ define(['libraries/WebWorldWind/src/WorldWind', }); }; + /** + * + */ + OSMBuildingLayer.prototype.addByFile = function (path) { + + }; + + /** + * + */ + OSMBuildingLayer.prototype.addByURL = function (path) { + + }; + return OSMBuildingLayer; }); diff --git a/src/OSMLayer.js b/src/OSMLayer.js index bd98ad7..1da4b46 100644 --- a/src/OSMLayer.js +++ b/src/OSMLayer.js @@ -1,7 +1,9 @@ /** * @exports OSMLayer */ -define(['libraries/WebWorldWind/src/WorldWind'], function (WorldWind) { +define(['libraries/WebWorldWind/src/error/ArgumentError', + 'libraries/WebWorldWind/src/util/Logger'], + function (ArgumentError, Logger) { "use strict"; /** @@ -10,13 +12,12 @@ define(['libraries/WebWorldWind/src/WorldWind'], function (WorldWind) { * @constructor * @classdesc Sets the properties and functions viable for any OSM data. It is intended to be an abstract class, only to be extended for specific OSM data. * @param {WorldWindow} worldWindow The WorldWindow where the OSMLayer is added to. - * @param {Float[]} boundingBox It defines the bounding box of the OSM data for the OSMLayer. * @param {Object} configuration Configuration is used to set the attributes of {@link PlacemarkAttributes} or {@link ShapeAttributes}. */ - var OSMLayer = function (worldWindow, boundingBox, configuration) { + var OSMLayer = function (worldWindow, configuration) { this._worldWindow = worldWindow; - this._boundingBox = boundingBox; this._configuration = configuration; + this._boundingBox = null; this._type = null; this._tag = null; }; @@ -33,17 +34,6 @@ define(['libraries/WebWorldWind/src/WorldWind'], function (WorldWind) { return this._worldWindow; } }, - /** - * It defines the bounding box of the OSM data for the OSMLayer. The order of coordinates of the bounding box is "x1, y1, x2, y2". - * @memberof OSMLayer.prototype - * @type {Float[]} - * @readonly - */ - boundingBox: { - get: function() { - return this._boundingBox; - } - }, /** * Configuration is used to set the attributes of {@link PlacemarkAttributes} if the geometry is Point or MultiPoint; or of {@link ShapeAttributes} otherwise. * @memberof OSMLayer.prototype @@ -57,6 +47,19 @@ define(['libraries/WebWorldWind/src/WorldWind'], function (WorldWind) { this._configuration = configuration; } }, + /** + * It defines the bounding box of the OSM data for the OSMLayer. The order of coordinates of the bounding box is "x1, y1, x2, y2". + * @memberof OSMLayer.prototype + * @type {Float[]} + */ + boundingBox: { + get: function() { + return this._boundingBox; + }, + set: function(boundingBox) { + this._boundingBox = boundingBox; + } + }, /** * The type of the OSM data. It can be "node", "way", or "relation". * @memberof OSMLayer.prototype @@ -113,16 +116,24 @@ define(['libraries/WebWorldWind/src/WorldWind'], function (WorldWind) { /** * 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}. + * @throws {ArgumentError} If boundingBox of the layer is null. */ OSMLayer.prototype.zoom = function () { - 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; - // console.log(centerX + ", " + centerY); - this.worldWindow.navigator.range = 1e4; // Should be automatically calculated. - this.worldWindow.redraw(); + 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; + // console.log(centerX + ", " + centerY); + this.worldWindow.navigator.range = 1e4; // Should be automatically calculated. + this.worldWindow.redraw(); + } + else { + throw new ArgumentError( + Logger.logMessage(Logger.LEVEL_SEVERE, "OSMLayer", "zoom", "The bounding box of the layer is null.") + ); + } }; return OSMLayer;