Skip to content

Commit b87f84d

Browse files
committed
Added new options for specifying which fields to download, specifying the object_id_field, and specifying the geoemtry type to download
1 parent b637a8b commit b87f84d

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

arcgis/arcgis.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class ArcGIS:
2020
could be possible further down the line.
2121
2222
"""
23-
def __init__(self, url):
23+
def __init__(self, url, geom_type=None, object_id_field="OBJECTID"):
2424
self.url=url
25+
self.object_id_field=object_id_field
2526
self._layer_descriptor_cache = {}
27+
self.geom_type=geom_type
2628
self._geom_parsers = {
2729
'esriGeometryPoint': self._parse_esri_point,
2830
'esriGeometryMultipoint': self._parse_esri_multipoint,
@@ -77,16 +79,18 @@ def get_json(self, layer, where="1 = 1", fields=[], count_only=False, srid='4326
7779
"""
7880
Gets the JSON file from ArcGIS
7981
"""
80-
response = requests.get(self._build_query_request(layer),
81-
params = {
82+
params = {
8283
'where': where,
8384
'outFields': ", ".join(fields),
8485
'returnGeometry': True,
8586
'outSR': srid,
8687
'f': "pjson",
87-
'orderByFields': "OBJECTID",
88+
'orderByFields': self.object_id_field,
8889
'returnCountOnly': count_only
89-
})
90+
}
91+
if self.geom_type:
92+
params.update({'geometryType': self.geom_type})
93+
response = requests.get(self._build_query_request(layer), params=params)
9094
return response.json()
9195

9296
def get_descriptor_for_layer(self, layer):
@@ -139,7 +143,7 @@ def get(self, layer, where="1 = 1", fields=[], count_only=False, srid='4326'):
139143
break
140144
# If we've hit the transfer limit we offset by the last OBJECTID
141145
# returned and keep moving along.
142-
where = "OBJECTID > %s" % features[-1]['properties'].get('OBJECTID')
146+
where = "%s > %s" % (self.object_id_field, features[-1]['properties'].get(self.object_id_field))
143147
if base_where != "1 = 1" :
144148
# If we have another WHERE filter we needed to tack that back on.
145149
where += " AND %s" % base_where

bin/arcgis-get

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@ if __name__ == "__main__":
1313
help="Every feature in the GeoJSON will contain the layer name it came from in the field named here.")
1414
parser.add_argument('--where', type=str, default="1 = 1",
1515
help="A SQL-like WHERE clause to filter the data.")
16+
parser.add_argument('--fields', type=str, nargs='+', default="*",
17+
help='A field or list of fields to download only. This is useful if you only need a small subset of a large dataset. You can set this to just those fields to pare down on download time and file size. Note that if you use this option, you MUST include OBJECTID as a field.')
18+
parser.add_argument('--object_id_field', type=str, default="OBJECTID",
19+
help="If your layer uses a non-standard OBJECTID field (it happens!) you must set it here.")
20+
parser.add_argument('--geom_type', type=str, default=None,
21+
help="Sometimes you need to be explicit about the geometry type you are returning. Must be one of esriGeometryPoint, esriGeometryMultiPoint, esriGeometryPolygon, esriGeometryMultiPoint.")
1622
parser.add_argument('--count_only', action='store_true',
1723
help="Returns only a count of the features that will be returned")
1824

1925
args = parser.parse_args()
2026

21-
arc = arcgis.ArcGIS(args.url)
27+
arc = arcgis.ArcGIS(args.url, geom_type=args.geom_type, object_id_field=args.object_id_field)
2228

2329
if len(args.layer) > 1:
2430
if args.count_only:
2531
print "Sorry, you can't run a count on multiple layers currently."
2632
else:
27-
print json.dumps(arc.getMultiple(args.layer, where=args.where, layer_name_field=args.layer_name_field))
33+
print json.dumps(arc.getMultiple(args.layer, where=args.where, fields=args.fields, layer_name_field=args.layer_name_field))
2834
else:
29-
print json.dumps(arc.get(args.layer[0], where=args.where, count_only=args.count_only))
35+
print json.dumps(arc.get(args.layer[0], where=args.where, fields=args.fields, count_only=args.count_only))

0 commit comments

Comments
 (0)