Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow GeoJSON extrusion #127

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion examples/GeoJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=properties.height || 1e5;

//Set altitude mode to relative
configuration.altitudeMode = WorldWind.RELATIVE_TO_GROUND;


return configuration;
};

var parserCompletionCallback = function(layer) {
Expand Down Expand Up @@ -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":{"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;
wwd.addLayer(sicilyBoundingsLayer);


// Create a layer manager for controlling layer visibility.
var layerManger = new LayerManager(wwd);
Expand Down
118 changes: 91 additions & 27 deletions src/formats/geojson/GeoJSONParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ define(['../../error/ArgumentError',
'./GeoJSONGeometryPolygon',
'../../geom/Location',
'../../util/Logger',
'../../shapes/Path',
'../../shapes/Placemark',
'../../shapes/PlacemarkAttributes',
'../../shapes/Polygon',
Expand All @@ -47,6 +48,7 @@ define(['../../error/ArgumentError',
GeoJSONGeometryPolygon,
Location,
Logger,
Path,
Placemark,
PlacemarkAttributes,
Polygon,
Expand Down Expand Up @@ -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}.
* <p>
Expand Down Expand Up @@ -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;
Expand All @@ -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}.
* <p>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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}.
* <p>
Expand Down Expand Up @@ -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) {
Expand All @@ -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}.
* <p>
Expand Down Expand Up @@ -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;
}
Expand Down