Skip to content
Permalink
Browse files

created a new class for triangulating the OSM polygons and reorganize…

…d the code
  • Loading branch information...
kilsedar committed Jul 10, 2017
1 parent 2a1904f commit 53d157c5d8f0bd7db5858c35ffbb2a6b3cfc0c9f
@@ -5,9 +5,9 @@ define(['libraries/WebWorldWind/src/WorldWind',
'libraries/WebWorldWind/src/formats/geojson/GeoJSONParser',
'libraries/WebWorldWind/src/geom/Position',
'libraries/WebWorldWind/src/shapes/TriangleMesh',
'src/OSMBuildingShape',
'src/shapes/BuildingShape',
'earcut'],
function (WorldWind, GeoJSONParser, Position, TriangleMesh, OSMBuildingShape, earcut) {
function (WorldWind, GeoJSONParser, Position, TriangleMesh, BuildingShape, earcut) {
"use strict";

/**
@@ -25,9 +25,8 @@ define(['libraries/WebWorldWind/src/WorldWind',

/**
* Invokes [lateralSurfaces]{@link GeoJSONParserTriangulation#lateralSurfaces} and/or [topSurface]{@link GeoJSONParserTriangulation#topSurface} to create a {@link TriangleMesh} for [Polygon]{@link GeoJSONGeometryPolygon} geometry.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry. [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback} is extended by four attributes in the {@link OSMBuildingLayer}.
* These attributes are "extrude", "heatmap", "altitude" and "altitudeMode".
* The altitude of the Polygon is set using this function using [setAltitude]{@link OSMBuildingShape#setAltitude}. If extrude and heatmap are enabled a new color is set for the Polygon.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry.
* It assumes extude and altitude is set in the configuration.
* If extrude is true, this function calls [lateralSurfaces]{@link GeoJSONParserTriangulation#lateralSurfaces} and [topSurface]{@link GeoJSONParserTriangulation#topSurface}. Otherwise it only calls [topSurface]{@link GeoJSONParserTriangulation#topSurface}.</p>
* Applications typically do not call this method directly. It is called by [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}.
* @param {RenderableLayer} layer The layer in which to place the newly created shapes.
@@ -51,19 +50,7 @@ define(['libraries/WebWorldWind/src/WorldWind',

var configuration = this.shapeConfigurationCallback(geometry, properties);
var boundaries = geometry._coordinates;
var OSMBuildingPolygon = new OSMBuildingShape(properties);
OSMBuildingPolygon.setAltitude(configuration);
var altitude = OSMBuildingPolygon.altitude;
if (configuration.extrude && configuration.heatmap.enabled)
OSMBuildingPolygon.setColor(configuration);

// console.log("configuration --> " + JSON.stringify(configuration));
// console.log("geometry --> " + JSON.stringify(geometry));
// console.log("boundaries --> " + boundaries);
// console.log("boundaries.length --> " + boundaries.length);
// console.log("properties --> " + JSON.stringify(properties));
// console.log("properties.tags.height --> " + properties.tags.height);
// console.log("altitude --> " + altitude);
var altitude = configuration.altitude;

if (!this.crs || this.crs.isCRSSupported()) {
if (configuration.extrude == true)
@@ -74,9 +61,8 @@ define(['libraries/WebWorldWind/src/WorldWind',

/**
* Invokes [lateralSurfaces]{@link GeoJSONParserTriangulation#lateralSurfaces} and/or [topSurface]{@link GeoJSONParserTriangulation#topSurface} to create a {@link TriangleMesh} for [MultiPolygon]{@link GeoJSONGeometryMultiPolygon} geometry.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry. [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback} is extended by three attributes in the {@link OSMBuildingLayer}.
* These attributes are "extrude", "heatmap", "altitude" and "altitudeMode".
* The altitude of the MultiPolygon is set using this function using [setAltitude]{@link OSMBuildingShape#setAltitude}. If extrude and heatmap are enabled a new color is set for the MultiPolygon.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry.
* It assumes extude and altitude is set in the configuration.
* If extrude is true, this function calls [lateralSurfaces]{@link GeoJSONParserTriangulation#lateralSurfaces} and [topSurface]{@link GeoJSONParserTriangulation#topSurface}. Otherwise it only calls [topSurface]{@link GeoJSONParserTriangulation#topSurface}.</p>
* Applications typically do not call this method directly. It is called by [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}.
* @param {RenderableLayer} layer The layer in which to place the newly created shapes.
@@ -85,7 +71,7 @@ define(['libraries/WebWorldWind/src/WorldWind',
* @throws {ArgumentError} If the specified layer is null or undefined.
* @throws {ArgumentError} If the specified geometry is null or undefined.
*/
GeoJSONParser.prototype.addRenderablesForMultiPolygon = function (layer, geometry, properties) {
GeoJSONParserTriangulation.prototype.addRenderablesForMultiPolygon = function (layer, geometry, properties) {
if (!layer) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "GeoJSON", "addRenderablesForMultiPolygon", "missingLayer")
@@ -100,15 +86,7 @@ define(['libraries/WebWorldWind/src/WorldWind',

var configuration = this.shapeConfigurationCallback(geometry, properties);
var polygons = geometry._coordinates, boundaries = [];
var OSMBuildingMultiPolygon = new OSMBuildingShape(properties);
OSMBuildingMultiPolygon.setAltitude(configuration);
var altitude = OSMBuildingMultiPolygon.altitude;
if (configuration.extrude && configuration.heatmap.enabled)
OSMBuildingMultiPolygon.setColor(configuration);

// console.log("properties --> " + JSON.stringify(properties));
// console.log("properties.tags.height (MultiPolygon) --> " + properties.tags.height);
// console.log("altitude --> " + altitude);
var altitude = configuration.altitude;

if (!this.crs || this.crs.isCRSSupported()) {
for (var polygonsIndex = 0; polygonsIndex < polygons.length; polygonsIndex++) {
@@ -122,7 +100,7 @@ define(['libraries/WebWorldWind/src/WorldWind',

/**
* Creates a {@link TriangleMesh} for the lateral surfaces of polygons. It creates two triangles for each lateral surface.
* @param {Object} configuration Configuration is the object returned by [shapeConfigurationCallback]{@link OSMBuildingLayer#shapeConfigurationCallback}.
* @param {Object} configuration Configuration is the object returned by shapeConfigurationCallback of GeoJSONParser or OSMBuildingLayer.
* @param {Object | Object[]} boundaries Boundaries of the polygons. If the geometry is [Polygon]{@link GeoJSONGeometryPolygon} the number of boundaries is one.
* If the geometry is [MultiPolygon]{@link GeoJSONGeometryMultiPolygon} the number of boundaries is more than one.
*/
@@ -171,7 +149,7 @@ define(['libraries/WebWorldWind/src/WorldWind',

/**
* Creates a {@link TriangleMesh} for the top surface of polygons, using earcut algorithm.
* @param {Object} configuration Configuration is the object returned by [shapeConfigurationCallback]{@link OSMBuildingLayer#shapeConfigurationCallback}.
* @param {Object} configuration Configuration is the object returned by shapeConfigurationCallback of GeoJSONParser or OSMBuildingLayer.
* @param {Object | Object[]} boundaries Boundaries of the polygons. If the geometry is [Polygon]{@link GeoJSONGeometryPolygon} the number of boundaries is one.
* If the geometry is [MultiPolygon]{@link GeoJSONGeometryMultiPolygon} the number of boundaries is more than one.
*/
@@ -210,11 +188,10 @@ define(['libraries/WebWorldWind/src/WorldWind',
* Invoked by [lateralSurfaces]{@link GeoJSONParserTriangulation#lateralSurfaces} or [topSurface]{@link GeoJSONParserTriangulation#topSurface}, it adds the {@link TriangleMesh} to the layer.
* @param {Position[]} positions Positions of the vertices of the triangles given in order, which means starting from index 0, every three vertices constitutes one triangle.
* @param {Integer[]} indices Indices of the positions in the positions array.
* @param {Object} configuration Configuration is the object returned by [shapeConfigurationCallback]{@link OSMBuildingLayer#shapeConfigurationCallback}.
* @param {Object} configuration Configuration is the object returned by shapeConfigurationCallback of GeoJSONParser or OSMBuildingLayer.
*/
GeoJSONParserTriangulation.prototype.addTriangleMesh = function (positions, indices, configuration) {
var shape = new TriangleMesh(positions, indices, configuration && configuration.attributes ? configuration.attributes : null);
// shapeConfigurationCallback sets only "configuration.attributes", not "altitudeMode". configuration object literal currently also returns "altitude" and "extrude".
shape.altitudeMode = configuration.altitudeMode;
if (configuration.highlightAttributes) {
shape.highlightAttributes = configuration.highlightAttributes;
@@ -0,0 +1,124 @@
/**
* @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) {
"use strict";

/**
* Creates a subclass of the {@link GeoJSONParserTriangulation} class.
* @alias GeoJSONParserTriangulationOSM
* @constructor
* @classdesc Triangulates polygons, which can be {@link Polygon}s or {@link MultiPolygon}s. Triangulated polygons improves rendering and painting performance compared to extruded polygons.
* @param {String} dataSource The data source in GeoJSON format. Can be a string or a URL for the data.
*/
var GeoJSONParserTriangulationOSM = function (dataSource) {
GeoJSONParserTriangulation.call(this, dataSource);
};

GeoJSONParserTriangulationOSM.prototype = Object.create(GeoJSONParserTriangulation.prototype);

/**
* Invokes [lateralSurfaces]{@link GeoJSONParserTriangulationOSM#lateralSurfaces} and/or [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface} to create a {@link TriangleMesh} for [Polygon]{@link GeoJSONGeometryPolygon} geometry.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry. [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback} is extended by four attributes in the {@link OSMBuildingLayer}.
* These attributes are "extrude", "heatmap", "altitude" and "altitudeMode".
* The altitude of the Polygon is set using this function using [setAltitude]{@link BuildingShape#setAltitude}. If extrude and heatmap are enabled a new color is set for the Polygon.
* If extrude is true, this function calls [lateralSurfaces]{@link GeoJSONParserTriangulationOSM#lateralSurfaces} and [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface}. Otherwise it only calls [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface}.</p>
* Applications typically do not call this method directly. It is called by [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}.
* @param {RenderableLayer} layer The layer in which to place the newly created shapes.
* @param {GeoJSONGeometryPolygon} geometry The Polygon geometry object.
* @param {Object} properties The properties related to the Polygon geometry.
* @throws {ArgumentError} If the specified layer is null or undefined.
* @throws {ArgumentError} If the specified geometry is null or undefined.
*/
GeoJSONParserTriangulationOSM.prototype.addRenderablesForPolygon = function (layer, geometry, properties) {
if (!layer) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "GeoJSON", "addRenderablesForPolygon", "missingLayer")
);
}

if (!geometry) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "GeoJSON", "addRenderablesForPolygon", "missingGeometry")
);
}

var configuration = this.shapeConfigurationCallback(geometry, properties);
var boundaries = geometry._coordinates;
var OSMBuildingPolygon = new BuildingShape(properties);
OSMBuildingPolygon.setAltitude(configuration);
var altitude = OSMBuildingPolygon.altitude;
if (configuration.extrude && configuration.heatmap.enabled)
OSMBuildingPolygon.setColor(configuration);

// console.log("configuration --> " + JSON.stringify(configuration));
// console.log("geometry --> " + JSON.stringify(geometry));
// console.log("boundaries --> " + boundaries);
// console.log("boundaries.length --> " + boundaries.length);
// console.log("properties --> " + JSON.stringify(properties));
// console.log("properties.tags.height --> " + properties.tags.height);
// console.log("altitude --> " + altitude);

if (!this.crs || this.crs.isCRSSupported()) {
if (configuration.extrude == true)
this.lateralSurfaces(configuration, altitude, boundaries);
this.topSurface(configuration, altitude, boundaries);
}
};

/**
* Invokes [lateralSurfaces]{@link GeoJSONParserTriangulationOSM#lateralSurfaces} and/or [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface} to create a {@link TriangleMesh} for [MultiPolygon]{@link GeoJSONGeometryMultiPolygon} geometry.
* <p>This method also invokes this GeoJSON's [shapeConfigurationCallback]{@link GeoJSONParser#shapeConfigurationCallback} for the geometry. [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback} is extended by three attributes in the {@link OSMBuildingLayer}.
* These attributes are "extrude", "heatmap", "altitude" and "altitudeMode".
* The altitude of the MultiPolygon is set using this function using [setAltitude]{@link BuildingShape#setAltitude}. If extrude and heatmap are enabled a new color is set for the MultiPolygon.
* If extrude is true, this function calls [lateralSurfaces]{@link GeoJSONParserTriangulationOSM#lateralSurfaces} and [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface}. Otherwise it only calls [topSurface]{@link GeoJSONParserTriangulationOSM#topSurface}.</p>
* Applications typically do not call this method directly. It is called by [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}.
* @param {RenderableLayer} layer The layer in which to place the newly created shapes.
* @param {GeoJSONGeometryMultiPolygon} geometry The MultiPolygon geometry object.
* @param {Object} properties The properties related to the MultiPolygon geometry.
* @throws {ArgumentError} If the specified layer is null or undefined.
* @throws {ArgumentError} If the specified geometry is null or undefined.
*/
GeoJSONParserTriangulationOSM.prototype.addRenderablesForMultiPolygon = function (layer, geometry, properties) {
if (!layer) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "GeoJSON", "addRenderablesForMultiPolygon", "missingLayer")
);
}

if (!geometry) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "GeoJSON", "addRenderablesForMultiPolygon", "missingGeometry")
);
}

var configuration = this.shapeConfigurationCallback(geometry, properties);
var polygons = geometry._coordinates, boundaries = [];
var OSMBuildingMultiPolygon = new BuildingShape(properties);
OSMBuildingMultiPolygon.setAltitude(configuration);
var altitude = OSMBuildingMultiPolygon.altitude;
if (configuration.extrude && configuration.heatmap.enabled)
OSMBuildingMultiPolygon.setColor(configuration);

// console.log("properties --> " + JSON.stringify(properties));
// console.log("properties.tags.height (MultiPolygon) --> " + properties.tags.height);
// console.log("altitude --> " + altitude);

if (!this.crs || this.crs.isCRSSupported()) {
for (var polygonsIndex = 0; polygonsIndex < polygons.length; polygonsIndex++) {
boundaries = polygons[polygonsIndex];
if (configuration.extrude == true)
this.lateralSurfaces(configuration, altitude, boundaries);
this.topSurface(configuration, altitude, boundaries);
}
}
};

return GeoJSONParserTriangulationOSM;
});
@@ -4,10 +4,10 @@
define(['libraries/WebWorldWind/src/WorldWind',
'libraries/WebWorldWind/src/cache/MemoryCache',
'src/OSMLayer',
'src/GeoJSONParserTriangulation',
'src/GeoJSONParserTriangulationOSM',
'jquery',
'osmtogeojson'],
function (WorldWind, MemoryCache, OSMLayer, GeoJSONParserTriangulation, $, osmtogeojson) {
function (WorldWind, MemoryCache, OSMLayer, GeoJSONParserTriangulationOSM, $, osmtogeojson) {
"use strict";

/**
@@ -81,7 +81,7 @@ define(['libraries/WebWorldWind/src/WorldWind',
var configuration = OSMLayer.prototype.shapeConfigurationCallback.call(this, geometry);

configuration.extrude = this.configuration.extrude ? this.configuration.extrude : false;
configuration.heatmap = this.configuration.heatmap ? this.configuration.heatmap : undefined;
configuration.heatmap = this.configuration.heatmap ? this.configuration.heatmap : false;
if (configuration.heatmap) {
configuration.heatmap.enabled = this.configuration.heatmap.enabled ? this.configuration.heatmap.enabled : false;
configuration.heatmap.thresholds = this.configuration.heatmap.thresholds ? this.configuration.heatmap.thresholds : [0, 15, 900];
@@ -96,7 +96,7 @@ 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 GeoJSONParserTriangulation}.
* adds the GeoJSON data to the WorldWindow using the {@link GeoJSONParserTriangulationOSM}.
*/
OSMBuildingLayer.prototype.add = function () {

@@ -126,7 +126,7 @@ define(['libraries/WebWorldWind/src/WorldWind',
// console.log("dataOverpassGeoJSON.features.length (number of polygons) --> " + dataOverpassGeoJSON.features.length);
// console.time("creatingOSMBuildingLayer");
var OSMBuildingLayer = new WorldWind.RenderableLayer("OSMBuildingLayer");
var OSMBuildingLayerGeoJSON = new GeoJSONParserTriangulation(dataOverpassGeoJSONString);
var OSMBuildingLayerGeoJSON = new GeoJSONParserTriangulationOSM(dataOverpassGeoJSONString);
// var OSMBuildingLayerGeoJSON = new WorldWind.GeoJSONParser(dataOverpassGeoJSONString);
OSMBuildingLayerGeoJSON.load(null, _self.shapeConfigurationCallback.bind(_self), OSMBuildingLayer);
// console.timeEnd("creatingOSMBuildingLayer");
Oops, something went wrong.

0 comments on commit 53d157c

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