Skip to content

Commit

Permalink
Added new options for specifying which fields to download, specifying…
Browse files Browse the repository at this point in the history
… the object_id_field, and specifying the geoemtry type to download
  • Loading branch information
schwanksta committed Sep 18, 2015
1 parent b637a8b commit b87f84d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 10 additions & 6 deletions arcgis/arcgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ class ArcGIS:
could be possible further down the line.
"""
def __init__(self, url):
def __init__(self, url, geom_type=None, object_id_field="OBJECTID"):
self.url=url
self.object_id_field=object_id_field
self._layer_descriptor_cache = {}
self.geom_type=geom_type
self._geom_parsers = {
'esriGeometryPoint': self._parse_esri_point,
'esriGeometryMultipoint': self._parse_esri_multipoint,
Expand Down Expand Up @@ -77,16 +79,18 @@ def get_json(self, layer, where="1 = 1", fields=[], count_only=False, srid='4326
"""
Gets the JSON file from ArcGIS
"""
response = requests.get(self._build_query_request(layer),
params = {
params = {
'where': where,
'outFields': ", ".join(fields),
'returnGeometry': True,
'outSR': srid,
'f': "pjson",
'orderByFields': "OBJECTID",
'orderByFields': self.object_id_field,
'returnCountOnly': count_only
})
}
if self.geom_type:
params.update({'geometryType': self.geom_type})
response = requests.get(self._build_query_request(layer), params=params)
return response.json()

def get_descriptor_for_layer(self, layer):
Expand Down Expand Up @@ -139,7 +143,7 @@ def get(self, layer, where="1 = 1", fields=[], count_only=False, srid='4326'):
break
# If we've hit the transfer limit we offset by the last OBJECTID
# returned and keep moving along.
where = "OBJECTID > %s" % features[-1]['properties'].get('OBJECTID')
where = "%s > %s" % (self.object_id_field, features[-1]['properties'].get(self.object_id_field))
if base_where != "1 = 1" :
# If we have another WHERE filter we needed to tack that back on.
where += " AND %s" % base_where
Expand Down
12 changes: 9 additions & 3 deletions bin/arcgis-get
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ if __name__ == "__main__":
help="Every feature in the GeoJSON will contain the layer name it came from in the field named here.")
parser.add_argument('--where', type=str, default="1 = 1",
help="A SQL-like WHERE clause to filter the data.")
parser.add_argument('--fields', type=str, nargs='+', default="*",
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.')
parser.add_argument('--object_id_field', type=str, default="OBJECTID",
help="If your layer uses a non-standard OBJECTID field (it happens!) you must set it here.")
parser.add_argument('--geom_type', type=str, default=None,
help="Sometimes you need to be explicit about the geometry type you are returning. Must be one of esriGeometryPoint, esriGeometryMultiPoint, esriGeometryPolygon, esriGeometryMultiPoint.")
parser.add_argument('--count_only', action='store_true',
help="Returns only a count of the features that will be returned")

args = parser.parse_args()

arc = arcgis.ArcGIS(args.url)
arc = arcgis.ArcGIS(args.url, geom_type=args.geom_type, object_id_field=args.object_id_field)

if len(args.layer) > 1:
if args.count_only:
print "Sorry, you can't run a count on multiple layers currently."
else:
print json.dumps(arc.getMultiple(args.layer, where=args.where, layer_name_field=args.layer_name_field))
print json.dumps(arc.getMultiple(args.layer, where=args.where, fields=args.fields, layer_name_field=args.layer_name_field))
else:
print json.dumps(arc.get(args.layer[0], where=args.where, count_only=args.count_only))
print json.dumps(arc.get(args.layer[0], where=args.where, fields=args.fields, count_only=args.count_only))

0 comments on commit b87f84d

Please sign in to comment.