diff --git a/arcgis/arcgis.py b/arcgis/arcgis.py index 905885d..4457465 100644 --- a/arcgis/arcgis.py +++ b/arcgis/arcgis.py @@ -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, @@ -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): @@ -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 diff --git a/bin/arcgis-get b/bin/arcgis-get index 009b590..0946915 100755 --- a/bin/arcgis-get +++ b/bin/arcgis-get @@ -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)) \ No newline at end of file + print json.dumps(arc.get(args.layer[0], where=args.where, fields=args.fields, count_only=args.count_only))