Skip to content

Commit

Permalink
Merge pull request #99 from taxProper-bryan/esrijson-output-format
Browse files Browse the repository at this point in the history
Adds "output_format" option to toggle between GeoJSON and EsriJSON output
  • Loading branch information
iandees authored Sep 26, 2023
2 parents d330a79 + f13b0ca commit 70fbe02
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion esridump/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def _parse_args(args):
action='store_true',
default=False,
help="Turn on paginate by OID regardless of normal pagination support")
parser.add_argument("--output-format",
dest='output_format',
action='store',
default='geojson',
help="The JSON output format of the feature data")

return parser.parse_args(args)

Expand All @@ -106,7 +111,8 @@ def main():
timeout=args.timeout,
max_page_size=args.max_page_size,
parent_logger=logger,
paginate_oid=args.paginate_oid)
paginate_oid=args.paginate_oid,
output_format=args.output_format)

if args.jsonlines:
for feature in dumper:
Expand Down
16 changes: 12 additions & 4 deletions esridump/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def __init__(self, url, parent_logger=None,
timeout=None, fields=None, request_geometry=True,
outSR=None, proxy=None,
start_with=None, geometry_precision=None,
paginate_oid=False,
max_page_size=None,
pause_seconds=10, requests_to_pause=5, num_of_retry=5):
paginate_oid=False, max_page_size=None,
pause_seconds=10, requests_to_pause=5,
num_of_retry=5, output_format='geojson'):
self._layer_url = url
self._query_params = extra_query_args or {}
self._headers = extra_headers or {}
Expand All @@ -35,6 +35,11 @@ def __init__(self, url, parent_logger=None,
self._requests_to_pause = requests_to_pause
self._num_of_retry = num_of_retry

if output_format not in ('geojson', 'esrijson'):
raise ValueError(f'Invalid output format. Expecting "geojson" or "esrijson", got {output_format}')

self._output_format = output_format

if parent_logger:
self._logger = parent_logger.getChild('esridump')
else:
Expand Down Expand Up @@ -502,4 +507,7 @@ def __iter__(self):
features = data.get('features')

for feature in features:
yield esri2geojson(feature)
if self._output_format == 'geojson':
yield esri2geojson(feature)
else:
yield feature
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def setUp(self):
self.parse_return.headers = []
self.parse_return.params = []
self.parse_return.proxy = None
self.parse_return.output_format = 'geojson'
self.mock_parseargs.return_value = self.parse_return

self.fake_url = 'http://example.com'
Expand Down
26 changes: 26 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,29 @@ def test_empty_result_set_short_circuits(self):
data = list(dump)

self.assertEqual(0, len(data))

def test_esri_json_output(self):
self.add_fixture_response(
r'.*/\?f=json.*',
'us-ca-carson/us-ca-carson-metadata.json',
method='GET',
)
self.add_fixture_response(
'.*returnCountOnly=true.*',
'us-ca-carson/us-ca-carson-count-only.json',
method='GET',
)
self.add_fixture_response(
'.*returnIdsOnly=true.*',
'us-ca-carson/us-ca-carson-ids-only.json',
method='GET',
)
self.add_fixture_response(
'.*query.*',
'us-ca-carson/us-ca-carson-0.json',
method='POST',
)

dump = EsriDumper(self.fake_url, output_format='esrijson')
data = list(dump)
self.assertIn('attributes', data[0], message='Data does not have "attributes" key with output format == esrijson')

0 comments on commit 70fbe02

Please sign in to comment.