#!/usr/bin/env python

import arcgis
import argparse
import json

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description="Output an ArcGIS web layer as GeoJSON")
	parser.add_argument('url', type=str,
                   help='The full url to an ArcGIS web service')
	parser.add_argument('layer', type=int,
                   help='The layer id within the service')
	parser.add_argument('--where', type=str, default="1 = 1",
					help="A SQL-like WHERE clause to filter the data.")
	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)
	
	print json.dumps(arc.get(args.layer, where=args.where, count_only=args.count_only))