Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Automatically compute bounding box for geojson.

  • Loading branch information...
commit 0128a84f83b55fe07c3ca824661a24f0c54ce9a7 1 parent e36e807
@onyxfish authored
View
17 csvkit/utilities/csvjson.py
@@ -37,6 +37,10 @@ def main(self):
if self.args.lat and self.args.lon:
features = []
+ min_lon = None
+ min_lat = None
+ max_lon = None
+ max_lat = None
lat_column = match_column_identifier(column_names, self.args.lat, self.args.zero_based)
lon_column = match_column_identifier(column_names, self.args.lon, self.args.zero_based)
@@ -56,8 +60,20 @@ def main(self):
for i, c in enumerate(row):
if i == lat_column:
lat = float(c)
+
+ if min_lat is None or lat < min_lat:
+ min_lat = lat
+
+ if max_lat is None or lat > max_lat:
+ max_lat = lat
elif i == lon_column:
lon = float(c)
+
+ if min_lon is None or lon < min_lon:
+ min_lon = lon
+
+ if max_lon is None or lon > max_lon:
+ max_lon = lon
elif id_column is not None and i == id_column:
geoid = c
else:
@@ -77,6 +93,7 @@ def main(self):
output = {
'type': 'FeatureCollection',
+ 'bbox': [min_lon, min_lat, max_lon, max_lat],
'features': features
}
elif self.args.key:
View
6 docs/scripts/csvjson.rst
@@ -71,6 +71,12 @@ Results in a GeoJSON document like::
{
"type": "FeatureCollection",
+ "bbox": [
+ -95.334619,
+ 32.299076986939205,
+ -95.250699,
+ 32.351434
+ ],
"features": [
{
"geometry": {
View
2  tests/test_utilities/test_csvjson.py
@@ -53,6 +53,7 @@ def test_geojson(self):
geojson = json.loads(output_file.getvalue())
self.assertEqual(geojson['type'], 'FeatureCollection')
+ self.assertEqual(geojson['bbox'], [-95.334619, 32.299076986939205, -95.250699, 32.351434])
self.assertEqual(len(geojson['features']), 17)
for feature in geojson['features']:
@@ -76,6 +77,7 @@ def test_geojson_with_id(self):
geojson = json.loads(output_file.getvalue())
self.assertEqual(geojson['type'], 'FeatureCollection')
+ self.assertEqual(geojson['bbox'], [-95.334619, 32.299076986939205, -95.250699, 32.351434])
self.assertEqual(len(geojson['features']), 17)
for feature in geojson['features']:
Please sign in to comment.
Something went wrong with that request. Please try again.