Skip to content

Commit

Permalink
Finish TopoJSON support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mramato committed Jun 28, 2013
1 parent a742386 commit 72dc8c4
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 59 deletions.
17 changes: 17 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ https://github.com/SteveSanderson/knockout-es5
>
> 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.
### topojson

https://github.com/mbostock/topojson

> Copyright (c) 2012, Michael Bostock
> All rights reserved.
>
> Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
>
> * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
>
> * 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.
>
> * The name Michael Bostock may not be used to endorse or promote products derived from this software without specific prior written permission.
>
> 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.
Tests
=====

Expand Down
28 changes: 8 additions & 20 deletions Source/DynamicScene/GeoJsonDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ define(['../Core/createGuid',
return value;
};

ConstantPositionProperty.prototype.setValue = function(value) {
this._value = value;
};

//GeoJSON specifies only the Feature object has a usable id property
//But since "multi" geometries create multiple dynamicObject,
//we can't use it for them either.
Expand Down Expand Up @@ -162,19 +158,11 @@ define(['../Core/createGuid',
}

function processTopology(dataSource, geoJson, geometry, crsFunction, source) {
var feature;
var typeHandler;
if (dataSource.renderTopoJsonAsMesh) {
feature = topojson.mesh(geometry);
typeHandler = geoJsonObjectTypes[feature.type];
typeHandler(dataSource, geometry, feature, crsFunction, source);
} else {
for ( var property in geometry.objects) {
if (geometry.objects.hasOwnProperty(property)) {
feature = topojson.feature(geometry, geometry.objects[property]);
typeHandler = geoJsonObjectTypes[feature.type];
typeHandler(dataSource, feature, feature, crsFunction, source);
}
for ( var property in geometry.objects) {
if (geometry.objects.hasOwnProperty(property)) {
var feature = topojson.feature(geometry, geometry.objects[property]);
var typeHandler = geoJsonObjectTypes[feature.type];
typeHandler(dataSource, feature, feature, crsFunction, source);
}
}
}
Expand Down Expand Up @@ -215,9 +203,9 @@ define(['../Core/createGuid',
};

/**
* A {@link DataSource} which processes GeoJSON. Since GeoJSON has no standard for styling content,
* we provide default graphics via the defaultPoint, defaultLine, and defaultPolygon properties.
* Any changes to these objects will affect the resulting {@link DynamicObject} collection.
* A {@link DataSource} which processes both GeoJSON and TopoJSON data. Since GeoJSON has no standard for styling
* content, we provide default graphics via the defaultPoint, defaultLine, and defaultPolygon properties. Any
* changes to these objects will affect the resulting {@link DynamicObject} collection.
* @alias GeoJsonDataSource
* @constructor
*
Expand Down
87 changes: 49 additions & 38 deletions Specs/Data/test.geojson
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
{
"type" : "FeatureCollection",
"features" : [
{
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [ 102.0, 0.5 ]
},
"properties" : {
"prop0" : "value0"
}
},
{
"type" : "Feature",
"geometry" : {
"type" : "LineString",
"coordinates" : [ [ 102.0, 0.0 ], [ 103.0, 1.0 ],
[ 104.0, 0.0 ], [ 105.0, 1.0 ] ]
},
"properties" : {
"prop0" : "value0",
"prop1" : 0.0
}
},
{
"type" : "Feature",
"geometry" : {
"type" : "Polygon",
"coordinates" : [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ],
[ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ]
},
"properties" : {
"prop0" : "value0",
"prop1" : {
"this" : "that"
}
}
} ]
}
"features" : [{
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [102.0, 0.5]
},
"properties" : {
"prop0" : "value0"
}
}, {
"type" : "Feature",
"geometry" : {
"type" : "LineString",
"coordinates" : [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]
},
"properties" : {
"prop0" : "value0",
"prop1" : 0.0
}
}, {
"type" : "Feature",
"geometry" : {
"type" : "Polygon",
"coordinates" : [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
},
"properties" : {
"prop0" : "value0",
"prop1" : {
"this" : "that"
}
}
}, {
"type" : "Feature",
"geometry" : {
"type" : "Topology",
"transform" : {
"scale" : [1, 1],
"translate" : [0, 0]
},
"objects" : {
"polygon" : {
"type" : "Polygon",
"arcs" : [[0, 1, 2, 3]]
}
},
"arcs" : [[[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], [[0, 0], [1, 0], [0, 1]], [[1, 1], [-1, 0], [0, -1]], [[1, 1]], [[0, 0]]]
}
}]
}
50 changes: 49 additions & 1 deletion Specs/DynamicScene/GeoJsonDataSourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ defineSuite(['DynamicScene/GeoJsonDataSource',
'geometries' : [unknownGeometry]
};

var topoJson = {
type : "Topology",
transform : {
scale : [1, 1],
translate : [0, 0]
},
objects : {
polygon : {
type : "Polygon",
arcs : [[0, 1, 2, 3]],
properties : {
myProps : 0
}
},
lineString : {
type : "LineString",
arcs : [4],
properties : {
myProps : 1
}
}
},
"arcs" : [[[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], [[0, 0], [1, 0], [0, 1]], [[1, 1], [-1, 0], [0, -1]], [[1, 1]], [[0, 0]]]
};

it('default constructor has expected values', function() {
var dataSource = new GeoJsonDataSource();
expect(dataSource.getChangedEvent()).toBeInstanceOf(Event);
Expand Down Expand Up @@ -339,6 +364,29 @@ defineSuite(['DynamicScene/GeoJsonDataSource',
});
});

it('Works with topojson geometry', function() {
var dataSource = new GeoJsonDataSource();
dataSource.load(topoJson);

var dynamicObjectCollection = dataSource.getDynamicObjectCollection();
waitsFor(function() {
return dynamicObjectCollection.getObjects().length === 2;
});
runs(function() {
var objects = dynamicObjectCollection.getObjects();

var polygon = objects[0];
expect(polygon.geoJson.properties).toBe(topoJson.objects.polygon.properties);
expect(polygon.vertexPositions).toBeDefined();
expect(polygon.polygon).toBeDefined();
expect(polygon.polyline).toBeDefined();

var lineString = objects[1];
expect(lineString.geoJson.properties).toBe(topoJson.objects.lineString.properties);
expect(lineString.polyline).toBeDefined();
});
});

it('Works with geometrycollection', function() {
var dataSource = new GeoJsonDataSource();
dataSource.load(geometryCollection);
Expand Down Expand Up @@ -404,7 +452,7 @@ defineSuite(['DynamicScene/GeoJsonDataSource',
dataSource.loadUrl('Data/test.geojson');

waitsFor(function() {
return dataSource.getDynamicObjectCollection().getObjects().length === 3;
return dataSource.getDynamicObjectCollection().getObjects().length === 4;
});
});

Expand Down

0 comments on commit 72dc8c4

Please sign in to comment.