Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased
----------

- fix: :doc:`/scripts/csvjson` uses 1-based numeric GeoJSON column identifiers by default and 0-based identifiers with :code:`--zero`, instead of the reverse.
- fix: :doc:`/scripts/csvgrep` treats an empty :code:`--regex` or :code:`--match` pattern as matching every value. Previously, with an empty pattern, :code:`--any-match` matched no rows, and :code:`--any-match --invert-match` matched every row.
- feat: :doc:`/scripts/csvcut` adds an :code:`--ignore-unknown-columns` option to skip identifiers in :code:`-c/--columns` that do not match a column in the input.
- feat: :doc:`/scripts/csvclean` adds a :code:`--remove-empty-columns` option to remove empty columns from standard output.
Expand Down
11 changes: 6 additions & 5 deletions csvkit/utilities/csvjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,24 @@ class GeoJsonGenerator:
def __init__(self, args, column_names):
self.args = args
self.column_names = column_names
column_offset = 0 if self.args.zero_based else 1

self.lat_column = match_column_identifier(column_names, self.args.lat, self.args.zero_based)
self.lat_column = match_column_identifier(column_names, self.args.lat, column_offset)

self.lon_column = match_column_identifier(column_names, self.args.lon, self.args.zero_based)
self.lon_column = match_column_identifier(column_names, self.args.lon, column_offset)

if self.args.type:
self.type_column = match_column_identifier(column_names, self.args.type, self.args.zero_based)
self.type_column = match_column_identifier(column_names, self.args.type, column_offset)
else:
self.type_column = None

if self.args.geometry:
self.geometry_column = match_column_identifier(column_names, self.args.geometry, self.args.zero_based)
self.geometry_column = match_column_identifier(column_names, self.args.geometry, column_offset)
else:
self.geometry_column = None

if self.args.key:
self.id_column = match_column_identifier(column_names, self.args.key, self.args.zero_based)
self.id_column = match_column_identifier(column_names, self.args.key, column_offset)
else:
self.id_column = None

Expand Down
3 changes: 3 additions & 0 deletions examples/test_geo_column_indices.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lat,lon,id,kind,shape,note
12,34,7,Point,"{""type"":""Point"",""coordinates"":[50,60]}",hello
-5,9,8,Point,"{""type"":""Point"",""coordinates"":[-2,3]}",world
57 changes: 57 additions & 0 deletions tests/test_utilities/test_csvjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from unittest.mock import patch

from csvkit.exceptions import ColumnIdentifierError
from csvkit.utilities.csvjson import CSVJSON, launch_new_instance
from tests.utils import CSVKitTestCase, EmptyFileTests

Expand Down Expand Up @@ -105,6 +106,62 @@ def test_geojson_with_id(self):
self.assertIsInstance(geometry['coordinates'][0], float)
self.assertIsInstance(geometry['coordinates'][1], float)

def test_geojson_column_indices(self):
self.check_geojson_column_indices(use_geometry=False)

def test_geojson_geometry_column_indices(self):
self.check_geojson_column_indices(use_geometry=True)

def check_geojson_column_indices(self, use_geometry):
selectors = (
('named', ['--lat', 'lat', '--lon', 'lon', '--type', 'kind', '--key', 'id'], 'shape'),
('one-based', ['--lat', '1', '--lon', '2', '--type', '4', '--key', '3'], '5'),
('zero-based', ['--zero', '--lat', '0', '--lon', '1', '--type', '3', '--key', '2'], '4'),
)
modes = (
('collection', ['-I', '-y', '0']),
('buffered-stream', ['--stream', '-I', '-y', '1024']),
('fast-stream', ['--stream', '-I', '-y', '0']),
)
shapes = [
{'type': 'Point', 'coordinates': [50, 60]},
{'type': 'Point', 'coordinates': [-2, 3]},
]
coordinates = [[50.0, 60.0], [-2.0, 3.0]] if use_geometry else [[34.0, 12.0], [9.0, -5.0]]
expected = []
for i, note in enumerate(('hello', 'world')):
properties = {'note': note}
if not use_geometry:
properties['shape'] = json.dumps(shapes[i], separators=(',', ':'))
expected.append({
'type': 'Feature', 'id': str(i + 7), 'properties': properties,
'geometry': {'type': 'Point', 'coordinates': coordinates[i]},
})
for selector, flags, geometry_column in selectors:
for mode, mode_flags in modes:
with self.subTest(selector=selector, mode=mode):
geometry_flags = ['--geometry', geometry_column] if use_geometry else []
output = self.get_output(mode_flags + flags + geometry_flags +
['examples/test_geo_column_indices.csv'])
if mode == 'collection':
parsed = json.loads(output)
self.assertEqual(parsed['type'], 'FeatureCollection')
self.assertEqual(parsed['bbox'], [-2.0, 3.0, 50.0, 60.0] if use_geometry
else [9.0, -5.0, 34.0, 12.0])
self.assertEqual(parsed['features'], expected)
else:
self.assertEqual([json.loads(line) for line in output.splitlines()], expected)

def test_geojson_column_index_errors(self):
for flags, message in (
(['--lat', '0'], 'Column 0 is invalid. Columns are 1-based.'),
(['--zero', '--lat', '6'], "Column 6 is invalid. The last column is 'note' at index 5."),
):
with self.subTest(flags=flags):
with self.assertRaisesRegex(ColumnIdentifierError, message):
self.get_output(['-I', '-y', '0', '--lon', 'lon'] + flags +
['examples/test_geo_column_indices.csv'])

def test_geojson_point(self):
geojson = json.loads(self.get_output(['--lat', 'latitude', '--lon', 'longitude', 'examples/test_geo.csv']))

Expand Down