Skip to content

Commit

Permalink
Merge pull request #1 from chelm/force-multi
Browse files Browse the repository at this point in the history
forcing all non-multi geometries to be multi
  • Loading branch information
ungoldman committed Mar 20, 2015
2 parents 294a096 + 416c41b commit 505c3f1
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 9 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ module.exports = function (collection, callback) {
if (!f.geometry) return

var isPoint = f.geometry.type === 'Point'
var isMultiPoint = f.geometry.type === 'MultiPoint'
var isLine = f.geometry.type === 'LineString'
var isMultiLine = f.geometry.type === 'MultiLineString'
var isPolygon = f.geometry.type === 'Polygon'
var isMultiPolygon = f.geometry.type === 'MultiPolygon'
var coords = f.geometry.coordinates

if (isPoint) extend(bbox, coords)
if (isPoint || isMultiPoint) {
if (isPoint) coords = [[[coords]]]
else coords = [[coords]]
loop(bbox, coords)
}

if (isLine || isMultiLine) {
if (isMultiLine) coords = coords[0]
coords.forEach(function (c, j) {
extend(bbox, c)
})
if (isLine) coords = [[coords]]
else coords = [coords]
loop(bbox, coords)
}

if (isPolygon || isMultiPolygon) {
coords = isMultiPolygon ? coords[0][0] : coords[0]
coords.forEach(function (c, j) {
extend(bbox, c)
})
if (isPolygon) coords = [coords]
loop(bbox, coords)
}
})

Expand All @@ -38,6 +40,16 @@ module.exports = function (collection, callback) {
else return extent
}

function loop (bbox, coords) {
coords.forEach(function (r) {
r.forEach(function (inner) {
inner.forEach(function (c) {
extend(bbox, c)
})
})
})
}

function extend (bbox, coord) {
bbox[0] = Math.min(bbox[0], coord[0])
bbox[1] = Math.min(bbox[1], coord[1])
Expand Down
131 changes: 131 additions & 0 deletions test/geojson-multi-spec-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
102.0,
0.5
],
[
106.0,
0.5
]
]
},
"properties": {
"prop0": "value0"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
102.0,
0.0
],
[
103.0,
1.0
],
[
104.0,
0.0
],
[
105.0,
1.0
]
],
[
[
99.0,
0.0
],
[
103.0,
1.0
],
[
104.0,
0.0
],
[
107.0,
2.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
]
],
[
[
98.0,
0.0
],
[
101.0,
0.0
],
[
101.0,
1.0
],
[
100.0,
1.0
],
[
98.0,
0.0
]
]
]
},
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
}
}
]
}
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var esriExtent = require('..')
var example = require('./geojson-spec-example.json')
var exampleMulti = require('./geojson-multi-spec-example.json')
var test = require('tape')
var expected = {
xmin: 100,
Expand All @@ -12,6 +13,17 @@ var expected = {
}
}

var expectedMulti = {
xmin: 98,
ymin: 0,
xmax: 107,
ymax: 2,
spatialReference: {
wkid: 4326,
latestWkid: 4326
}
}

test('get correct extent from geojson spec example', function (t) {
esriExtent(example, function (err, extent) {
if (err) throw err
Expand All @@ -20,3 +32,12 @@ test('get correct extent from geojson spec example', function (t) {
t.end()
})
})

test('get correct extent from geojson multi spec example', function (t) {
esriExtent(exampleMulti, function (err, extent) {
if (err) throw err

t.deepEqual(extent, expectedMulti)
t.end()
})
})

0 comments on commit 505c3f1

Please sign in to comment.