Skip to content

Commit 1051552

Browse files
committed
Merge pull request #906 from AnalyticalGraphicsInc/topojson
Add TopoJSON support to GeoJsonDataSource
2 parents 07352a2 + 2cca588 commit 1051552

File tree

8 files changed

+507
-75
lines changed

8 files changed

+507
-75
lines changed

Apps/CesiumViewer/CesiumViewer.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -84,34 +84,42 @@ define([
8484

8585
if (typeof endUserOptions.source !== 'undefined') {
8686
var source;
87-
if (endsWith(endUserOptions.source.toUpperCase(), ".GEOJSON")) {
87+
var sourceUrl = endUserOptions.source.toUpperCase();
88+
if (endsWith(sourceUrl, ".GEOJSON") || //
89+
endsWith(sourceUrl, ".JSON") || //
90+
endsWith(sourceUrl, ".TOPOJSON")) {
8891
source = new GeoJsonDataSource();
89-
} else {
92+
} else if (endsWith(sourceUrl, ".CZML")) {
9093
source = new CzmlDataSource();
94+
} else {
95+
loadingIndicator.style.display = 'none';
96+
window.alert("Unknown format: " + endUserOptions.source);
9197
}
92-
source.loadUrl(endUserOptions.source).then(function() {
93-
viewer.dataSources.add(source);
98+
if (typeof source !== 'undefined') {
99+
source.loadUrl(endUserOptions.source).then(function() {
100+
viewer.dataSources.add(source);
94101

95-
var dataClock = source.getClock();
96-
if (typeof dataClock !== 'undefined') {
97-
dataClock.clone(viewer.clock);
98-
viewer.timeline.updateFromClock();
99-
viewer.timeline.zoomTo(dataClock.startTime, dataClock.stopTime);
100-
}
102+
var dataClock = source.getClock();
103+
if (typeof dataClock !== 'undefined') {
104+
dataClock.clone(viewer.clock);
105+
viewer.timeline.updateFromClock();
106+
viewer.timeline.zoomTo(dataClock.startTime, dataClock.stopTime);
107+
}
101108

102-
if (typeof endUserOptions.lookAt !== 'undefined') {
103-
var dynamicObject = source.getDynamicObjectCollection().getObject(endUserOptions.lookAt);
104-
if (typeof dynamicObject !== 'undefined') {
105-
viewer.trackedObject = dynamicObject;
106-
} else {
107-
window.alert('No object with id ' + endUserOptions.lookAt + ' exists in the provided source.');
109+
if (typeof endUserOptions.lookAt !== 'undefined') {
110+
var dynamicObject = source.getDynamicObjectCollection().getObject(endUserOptions.lookAt);
111+
if (typeof dynamicObject !== 'undefined') {
112+
viewer.trackedObject = dynamicObject;
113+
} else {
114+
window.alert('No object with id ' + endUserOptions.lookAt + ' exists in the provided source.');
115+
}
108116
}
109-
}
110-
}, function(e) {
111-
window.alert(e);
112-
}).always(function() {
113-
loadingIndicator.style.display = 'none';
114-
});
117+
}, function(e) {
118+
window.alert(e);
119+
}).always(function() {
120+
loadingIndicator.style.display = 'none';
121+
});
122+
}
115123
} else {
116124
loadingIndicator.style.display = 'none';
117125
}

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Beta Releases
1616
* Removed `CesiumViewerWidget` and replaced it with a new `Viewer` widget with mixin architecture. This new widget does not depend on Dojo and is part of the combined Cesium.js file. It is intended to be a flexible base widget for easily building robust applications. See [#838](https://github.com/AnalyticalGraphicsInc/cesium/pull/838) for the full details.
1717
* Removed the Dojo-based `checkForChromeFrame` function, and replaced it with a new standalone version that returns a promise to signal when the asynchronous check has completed.
1818
* Fix resizing issues in `CesiumWidget` ([#608](https://github.com/AnalyticalGraphicsInc/cesium/issues/608), [#834](https://github.com/AnalyticalGraphicsInc/cesium/issues/834)).
19-
* Added initial support for [GeoJSON](http://www.geojson.org/) see [#890](https://github.com/AnalyticalGraphicsInc/cesium/pull/890) for details.
19+
* Added initial support for [GeoJSON](http://www.geojson.org/) and [TopoJSON](https://github.com/mbostock/topojson). See [#890](https://github.com/AnalyticalGraphicsInc/cesium/pull/890) and [#906](https://github.com/AnalyticalGraphicsInc/cesium/pull/906) for details.
2020
* Added `Context.getAntialias`.
2121
* Added rotation, aligned axis, width, and height properties to `Billboard`s.
2222
* Improved the performance of "missing tile" checking, especially for Bing imagery.

LICENSE.md

+17
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ https://github.com/SteveSanderson/knockout-es5
190190
>
191191
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
192192
193+
### topojson
194+
195+
https://github.com/mbostock/topojson
196+
197+
> Copyright (c) 2012, Michael Bostock
198+
> All rights reserved.
199+
>
200+
> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
201+
>
202+
> * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
203+
>
204+
> * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
205+
>
206+
> * The name Michael Bostock may not be used to endorse or promote products derived from this software without specific prior written permission.
207+
>
208+
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209+
193210
Tests
194211
=====
195212

Source/DynamicScene/GeoJsonDataSource.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ define(['../Core/createGuid',
1515
'./DynamicPolygon',
1616
'./DynamicMaterialProperty',
1717
'./DynamicObjectCollection',
18-
'../ThirdParty/when'], function(
18+
'../ThirdParty/when',
19+
'../ThirdParty/topojson'], function(
1920
createGuid,
2021
Cartographic,
2122
Color,
@@ -32,7 +33,8 @@ define(['../Core/createGuid',
3233
DynamicPolygon,
3334
DynamicMaterialProperty,
3435
DynamicObjectCollection,
35-
when) {
36+
when,
37+
topojson) {
3638
"use strict";
3739

3840
//DynamicPositionProperty is pretty hard to use with non-CZML based data
@@ -50,10 +52,6 @@ define(['../Core/createGuid',
5052
return value;
5153
};
5254

53-
ConstantPositionProperty.prototype.setValue = function(value) {
54-
this._value = value;
55-
};
56-
5755
//GeoJSON specifies only the Feature object has a usable id property
5856
//But since "multi" geometries create multiple dynamicObject,
5957
//we can't use it for them either.
@@ -159,6 +157,16 @@ define(['../Core/createGuid',
159157
dynamicObject.vertexPositions = new ConstantPositionProperty(coordinatesArrayToCartesianArray(geometry.coordinates[0], crsFunction));
160158
}
161159

160+
function processTopology(dataSource, geoJson, geometry, crsFunction, source) {
161+
for ( var property in geometry.objects) {
162+
if (geometry.objects.hasOwnProperty(property)) {
163+
var feature = topojson.feature(geometry, geometry.objects[property]);
164+
var typeHandler = geoJsonObjectTypes[feature.type];
165+
typeHandler(dataSource, feature, feature, crsFunction, source);
166+
}
167+
}
168+
}
169+
162170
function processMultiPolygon(dataSource, geoJson, geometry, crsFunction, source) {
163171
//TODO holes
164172
var polygons = geometry.coordinates;
@@ -179,7 +187,8 @@ define(['../Core/createGuid',
179187
MultiPoint : processMultiPoint,
180188
MultiPolygon : processMultiPolygon,
181189
Point : processPoint,
182-
Polygon : processPolygon
190+
Polygon : processPolygon,
191+
Topology : processTopology
183192
};
184193

185194
var geometryTypes = {
@@ -189,13 +198,14 @@ define(['../Core/createGuid',
189198
MultiPoint : processMultiPoint,
190199
MultiPolygon : processMultiPolygon,
191200
Point : processPoint,
192-
Polygon : processPolygon
201+
Polygon : processPolygon,
202+
Topology : processTopology
193203
};
194204

195205
/**
196-
* A {@link DataSource} which processes GeoJSON. Since GeoJSON has no standard for styling content,
197-
* we provide default graphics via the defaultPoint, defaultLine, and defaultPolygon properties.
198-
* Any changes to these objects will affect the resulting {@link DynamicObject} collection.
206+
* A {@link DataSource} which processes both GeoJSON and TopoJSON data. Since GeoJSON has no standard for styling
207+
* content, we provide default graphics via the defaultPoint, defaultLine, and defaultPolygon properties. Any
208+
* changes to these objects will affect the resulting {@link DynamicObject} collection.
199209
* @alias GeoJsonDataSource
200210
* @constructor
201211
*

0 commit comments

Comments
 (0)