From 8315ad00e30cba827d2910412abdf2c6f7caf34e Mon Sep 17 00:00:00 2001 From: GabrielePrestifilippo Date: Tue, 23 May 2017 20:17:39 +0200 Subject: [PATCH 1/2] GeoJSON Extrusion --- examples/GeoJSON.js | 53 +++++++++++- src/formats/geojson/GeoJSONParser.js | 118 +++++++++++++++++++++------ 2 files changed, 143 insertions(+), 28 deletions(-) diff --git a/examples/GeoJSON.js b/examples/GeoJSON.js index eb181d6b8..bd9145628 100644 --- a/examples/GeoJSON.js +++ b/examples/GeoJSON.js @@ -76,7 +76,38 @@ requirejs(['../src/WorldWind', 1.0); } - return configuration; + return configuration; + }; + + var extrudedConfigurationCallback = function (geometry, properties) { + var configuration = {}; + + configuration.attributes = new WorldWind.ShapeAttributes(null); + + // Fill the polygon with a random pastel color. + configuration.attributes.interiorColor = new WorldWind.Color( + 0.375 + 0.5 * Math.random(), + 0.375 + 0.5 * Math.random(), + 0.375 + 0.5 * Math.random(), + 1.0); + + configuration.attributes.outlineColor = new WorldWind.Color( + 0.5 * configuration.attributes.interiorColor.red, + 0.5 * configuration.attributes.interiorColor.green, + 0.5 * configuration.attributes.interiorColor.blue, + 1.0); + + //Extrude the polygon + configuration.extrude = true; + + //Set the altitude for the extrusion + configuration.altitude=1e5; + + //Set altitude mode to relative + configuration.altitudeMode = WorldWind.RELATIVE_TO_GROUND; + + + return configuration; }; var parserCompletionCallback = function(layer) { @@ -173,6 +204,26 @@ requirejs(['../src/WorldWind', usa4326GeoJSON.load(null, shapeConfigurationCallback, usa4326Layer); wwd.addLayer(usa4326Layer); + //Parsing GeoJSON directly + + //Italy Bounding Box + var italyBoundingsLayer = new WorldWind.RenderableLayer("Italy Boundings"); + var italyBoundingsGeoJSON = new WorldWind.GeoJSONParser('{"type":"FeatureCollection","features":[{"type":"Feature",' + + '"properties":{},"geometry":{"type":"Polygon","coordinates":[[[6.30615,36.50963],[18.63281,36.50963],' + + '[18.63281,46.81509],[6.30615,46.81509],[6.30615,36.50963]]]}}]}'); + italyBoundingsGeoJSON.load(null, shapeConfigurationCallback, italyBoundingsLayer); + italyBoundingsLayer.enabled = false; + wwd.addLayer(italyBoundingsLayer); + + //Sicily Bounding Box Extruded + var sicilyBoundingsLayer = new WorldWind.RenderableLayer("Sicily Boundings"); + var sicilyBoundingsGeoJSON = new WorldWind.GeoJSONParser('{"type":"FeatureCollection","features":[{"type":"' + + 'Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[12.07397,36.55377],' + + '[15.72143,36.55377],[15.72143,38.48799],[12.07397,38.48799],[12.07397,36.55377]]]}}]}'); + sicilyBoundingsGeoJSON.load(null, extrudedConfigurationCallback, sicilyBoundingsLayer); + sicilyBoundingsLayer.enabled = false; + wwd.addLayer(sicilyBoundingsLayer); + // Create a layer manager for controlling layer visibility. var layerManger = new LayerManager(wwd); diff --git a/src/formats/geojson/GeoJSONParser.js b/src/formats/geojson/GeoJSONParser.js index edf1e9e9e..a0a5722ad 100644 --- a/src/formats/geojson/GeoJSONParser.js +++ b/src/formats/geojson/GeoJSONParser.js @@ -21,6 +21,7 @@ define(['../../error/ArgumentError', './GeoJSONGeometryPolygon', '../../geom/Location', '../../util/Logger', + '../../shapes/Path', '../../shapes/Placemark', '../../shapes/PlacemarkAttributes', '../../shapes/Polygon', @@ -47,6 +48,7 @@ define(['../../error/ArgumentError', GeoJSONGeometryPolygon, Location, Logger, + Path, Placemark, PlacemarkAttributes, Polygon, @@ -654,7 +656,9 @@ define(['../../error/ArgumentError', }; /** - * Creates a {@link SurfacePolyline} for a LineString geometry. + * Creates a {@link Path}s or a {@link SurfacePolyline} for a LineString geometry, + * depending on the altitude optionally returned by the + * [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback}. * Applications typically do not call this method directly. It is called by * [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}. *

@@ -685,22 +689,34 @@ define(['../../error/ArgumentError', var positions = []; for (var pointsIndex = 0, points = geometry.coordinates; pointsIndex < points.length; pointsIndex++) { var longitude = points[pointsIndex][0], - latitude = points[pointsIndex][1]; + latitude = points[pointsIndex][1], //altitude = points[pointsIndex][2] ? points[pointsIndex][2] : 0, + altitude = configuration && configuration.altitude ? configuration.altitude : null, + position; var reprojectedCoordinate = this.getReprojectedIfRequired( latitude, longitude, this.crs); - var position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + + if (altitude) { + position = new Position(reprojectedCoordinate[1], reprojectedCoordinate[0], altitude); + } else { + position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + } positions.push(position); } var shape; - shape = new SurfacePolyline( - positions, - configuration && configuration.attributes ? configuration.attributes : null); - if (configuration.highlightAttributes) { - shape.highlightAttributes = configuration.highlightAttributes; + if (altitude) { + shape = new Path(positions, configuration && configuration.attributes ? configuration.attributes : null); + if (configuration && configuration.extrude) { + shape.extrude = configuration.extrude; + } + shape.altitudeMode = configuration.altitudeMode || WorldWind.RELATIVE_TO_GROUND; + } else { + shape = new SurfacePolyline( + positions, + configuration && configuration.attributes ? configuration.attributes : null); } if (configuration && configuration.pickDelegate) { shape.pickDelegate = configuration.pickDelegate; @@ -713,7 +729,9 @@ define(['../../error/ArgumentError', }; /** - * Creates {@link SurfacePolyline}s for a MultiLineString geometry. + * Creates a {@link Path}s or {@link SurfacePolyline}s for a MultiLineString geometry, + * depending on the altitude optionally returned by the + * [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback}. * Applications typically do not call this method directly. It is called by * [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}. *

@@ -747,20 +765,34 @@ define(['../../error/ArgumentError', for (var positionIndex = 0, points = lines[linesIndex]; positionIndex < points.length; positionIndex++) { var longitude = points[positionIndex][0], - latitude = points[positionIndex][1]; + latitude = points[positionIndex][1], //altitude = points[positionIndex][2] ? points[positionIndex][2] : 0, + altitude = configuration && configuration.altitude ? configuration.altitude : null, + position; var reprojectedCoordinate = this.getReprojectedIfRequired( latitude, longitude, this.crs); - var position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + if (altitude) { + position = new Position(reprojectedCoordinate[1], reprojectedCoordinate[0], altitude); + } else { + position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + } positions.push(position); } var shape; - shape = new SurfacePolyline( - positions, - configuration && configuration.attributes ? configuration.attributes : null); + if (altitude) { + shape = new Path(positions, configuration && configuration.attributes ? configuration.attributes : null); + if (configuration && configuration.extrude) { + shape.extrude = configuration.extrude; + } + shape.altitudeMode = configuration.altitudeMode || WorldWind.RELATIVE_TO_GROUND; + } else { + shape = new SurfacePolyline( + positions, + configuration && configuration.attributes ? configuration.attributes : null); + } if (configuration.highlightAttributes) { shape.highlightAttributes = configuration.highlightAttributes; } @@ -776,7 +808,9 @@ define(['../../error/ArgumentError', }; /** - * Creates a {@link SurfacePolygon} for a Polygon geometry. + * Creates a {@link Polygon}s or {@link SurfacePolygon} for a Polygon geometry, + * depending on the altitude optionally returned by the + * [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback}. * Applications typically do not call this method directly. It is called by * [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}. *

@@ -809,21 +843,35 @@ define(['../../error/ArgumentError', for (var positionIndex = 0, points = boundaries[boundariesIndex]; positionIndex < points.length; positionIndex++) { var longitude = points[positionIndex][0], - latitude = points[positionIndex][1]; + latitude = points[positionIndex][1], //altitude = points[positionIndex][2] ? points[positionIndex][2] : 0, + altitude = configuration && configuration.altitude ? configuration.altitude : null, + position; var reprojectedCoordinate = this.getReprojectedIfRequired( latitude, longitude, this.crs); - var position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + if (altitude) { + position = new Position(reprojectedCoordinate[1], reprojectedCoordinate[0], altitude); + } else { + position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + } positions.push(position); } var shape; - shape = new SurfacePolygon( - positions, - configuration && configuration.attributes ? configuration.attributes : null); - if (configuration.highlightAttributes) { + if (altitude) { + shape = new Polygon(positions, configuration && configuration.attributes ? configuration.attributes : null); + if (configuration && configuration.extrude) { + shape.extrude = configuration.extrude; + } + shape.altitudeMode = configuration.altitudeMode || WorldWind.RELATIVE_TO_GROUND; + } else { + shape = new SurfacePolygon( + positions, + configuration && configuration.attributes ? configuration.attributes : null); + } + if (configuration.highlightAttributes) { shape.highlightAttributes = configuration.highlightAttributes; } if (configuration && configuration.pickDelegate) { @@ -837,7 +885,9 @@ define(['../../error/ArgumentError', }; /** - * Creates {@link SurfacePolygon}s for a MultiPolygon geometry. + * Creates {@link Polygon}s or {@link SurfacePolygon}s for a MultiPolygon geometry, + * depending on the altitude optionally returned by the + * [shapeConfigurationCallback]{@link Shapefile#shapeConfigurationCallback}. * Applications typically do not call this method directly. It is called by * [addRenderablesForGeometry]{@link GeoJSONParser#addRenderablesForGeometry}. *

@@ -873,22 +923,36 @@ define(['../../error/ArgumentError', for (var positionIndex = 0, points = polygons[polygonsIndex][boundariesIndex]; positionIndex < points.length; positionIndex++) { var longitude = points[positionIndex][0], - latitude = points[positionIndex][1]; + latitude = points[positionIndex][1], //altitude = points[positionIndex][2] ? points[positionIndex][2] : 0,; + altitude = configuration && configuration.altitude ? configuration.altitude : null, + position; var reprojectedCoordinate = this.getReprojectedIfRequired( latitude, longitude, this.crs); - var position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + if (altitude) { + position = new Position(reprojectedCoordinate[1], reprojectedCoordinate[0], altitude); + } else { + position = new Location(reprojectedCoordinate[1], reprojectedCoordinate[0]); + } positions.push(position); } boundaries.push(positions); } var shape; - shape = new SurfacePolygon( - boundaries, - configuration && configuration.attributes ? configuration.attributes : null); + if (altitude) { + shape = new Polygon(positions, configuration && configuration.attributes ? configuration.attributes : null); + if (configuration && configuration.extrude) { + shape.extrude = configuration.extrude; + } + shape.altitudeMode = configuration.altitudeMode || WorldWind.RELATIVE_TO_GROUND; + } else { + shape = new SurfacePolygon( + positions, + configuration && configuration.attributes ? configuration.attributes : null); + } if (configuration.highlightAttributes) { shape.highlightAttributes = configuration.highlightAttributes; } From f7dd4b388acee14f0b7f23ec4612a0a774da62fb Mon Sep 17 00:00:00 2001 From: GabrielePrestifilippo Date: Thu, 25 May 2017 00:43:54 +0200 Subject: [PATCH 2/2] enable height from custom property --- examples/GeoJSON.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/GeoJSON.js b/examples/GeoJSON.js index bd9145628..c0193b2f5 100644 --- a/examples/GeoJSON.js +++ b/examples/GeoJSON.js @@ -101,7 +101,7 @@ requirejs(['../src/WorldWind', configuration.extrude = true; //Set the altitude for the extrusion - configuration.altitude=1e5; + configuration.altitude=properties.height || 1e5; //Set altitude mode to relative configuration.altitudeMode = WorldWind.RELATIVE_TO_GROUND; @@ -218,7 +218,7 @@ requirejs(['../src/WorldWind', //Sicily Bounding Box Extruded var sicilyBoundingsLayer = new WorldWind.RenderableLayer("Sicily Boundings"); var sicilyBoundingsGeoJSON = new WorldWind.GeoJSONParser('{"type":"FeatureCollection","features":[{"type":"' + - 'Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[12.07397,36.55377],' + + 'Feature","properties":{"height":"1e5"},"geometry":{"type":"Polygon","coordinates":[[[12.07397,36.55377],' + '[15.72143,36.55377],[15.72143,38.48799],[12.07397,38.48799],[12.07397,36.55377]]]}}]}'); sicilyBoundingsGeoJSON.load(null, extrudedConfigurationCallback, sicilyBoundingsLayer); sicilyBoundingsLayer.enabled = false;