def query_layer(token, filter="1=1"): """ Returns a mapping of 2 members: features and fields features is a list of records. A record is a dict with an "attributes" record and a "geometry" record. Ex: {u'features': [{u'attributes': {u'CreationDate': 1465804777348, u'Creator': u'villamig_ago', u'EditDate': 1465804777348, u'Editor': u'villamig_ago', u'FID': 1, u'area': u'Atlantic', u'casestudyf': u'CASESEARCH;', u'client_cls': u'featured', u'desc_': u'<p>The Albert canal in the eastern part of Flanders connects the industrial zones around Liege with ...</p>', u'featured': u'yes', u'itemname': u'New locks in Albertkanaal in Flanders, Belgium', u'measureid': 5601, u'newitem': u'no', u'risks': u'DROUGHT;', u'sectors': u'INFRASTRUCTURE;WATERMANAGEMENT;', u'website': u'www.descheepvaart.be/english.aspx;www.amice-project.eu/en/amice-project.php?refaction=31;www.madeinkempen.be/nieuws/reusachtige-vijzels-voor-pompinstallatie-aan-sluizencomplex-olen'}, u'geometry': {u'x': 560130.5, u'y': 6626173.1}}, ... u'fields': [{u'alias': u'FID', u'defaultValue': None, u'domain': None, u'name': u'FID', u'sqlType': u'sqlTypeInteger', u'type': u'esriFieldTypeInteger'}, ... } Fields are: [u'FID', u'area', u'itemname', u'desc_', u'website', u'sectors', u'risks', u'measureid', u'featured', u'newitem', u'casestudyf', u'client_cls', u'CreationDate', u'Creator', u'EditDate', u'Editor'] """ layer_url = get_feature_url() data = { 'f': 'json', 'useGlobalIds': True, 'where': filter, # 'outFields': 'objectid,itemname,measureid,FID,globalid', #"*" 'outFields': '*', 'token': token, 'referer': REFERER } url = "{0}/query".format(layer_url) resp = requests.get(url, params=data) return resp.json()
def apply_edits(data, op='updates', token=None): """ Makes a applyEdits request to arcgis See http://resources.arcgis.com/en/help/arcgis-rest-api/ for help """ settings = get_settings() if token is None: token = get_auth_token(settings) layer_url = get_feature_url(settings) data = { 'f': 'json', 'token': token, 'referer': REFERER, op: data, } url = "{0}/applyEdits".format(layer_url) resp = requests.post(url, data=data) res = resp.json() return res
def main(): # Needs env vars: # LD_LIBRARY_PATH=<buildout-directory>/parts/gdal-compile/lib # GISPASS='' token = get_auth_token() if sys.argv[1] == 'url': url = get_feature_url() #url = _get_token_service_url(endpoint) token = get_auth_token() print print "Token:", token print print "Feature URL: ", url + "?token=" + token print print "Query URL: ", url + "/query?token=" + token print elif sys.argv[1] == 'dump': print "Dumping..." if len(sys.argv) > 2: path = sys.argv[2] else: path = 'out.xml' res = query_layer(token=token) backup_data(res['features'], path=path) elif sys.argv[1] == 'summary': res = query_layer(token=token) print "Summary {0} entries...".format(len(res['features'])) for entry in res['features']: geo = '{0} x {1}'.format(*entry['geometry'].values()) attr = entry['attributes'] print attr['FID'], ': ', attr['itemname'], ' @ ', geo elif sys.argv[1] == 'del': print "Deleting..." fid = sys.argv[2] res = delete_casestudy(int(fid)) print res elif sys.argv[1] == 'delall': print "Deleting all..." print delete_all_casestudies(token) elif sys.argv[1] == 'addall': path = sys.argv[2] print add_all_casestudies(path, token) elif sys.argv[1] == 'edit': print "Editing..." fid = sys.argv[2] path = sys.argv[3] edit_casestudy(fid, path, token) elif sys.argv[1] == 'getfid': print "Getting FID for measureid..." measureid = sys.argv[2] print "FID: ", _get_obj_FID(uid=int(measureid)) else: print "Invalid command"
def main(): # Needs env vars: # LD_LIBRARY_PATH=<buildout-directory>/parts/gdal-compile/lib # GISPASS='' parser = argparse.ArgumentParser( description="Manual operations with ArcGIS" ) parser.add_argument("-u", "--get-feature-url", action="store_true") parser.add_argument("-d", "--dump", action="store_true") parser.add_argument("-s", "--summary", action="store_true") parser.add_argument("-x", "--delete", action="store_true") parser.add_argument("-g", "--get-fid", action="store_true") parser.add_argument("fid", nargs="?", type=int, default=None) parser.add_argument("-X", "--delete-all", action="store_true") parser.add_argument("-f", "--import-file", type=str, action="store") parser.add_argument("-e", "--edit-file", type=str, action="store") args = parser.parse_args() passwd = os.environ.get('GISPASS') if not passwd: logger.error("You need to provide the GISPASS env variable") sys.exit(1) token = get_auth_token() if args.get_feature_url: url = get_feature_url() # url = _get_token_service_url(endpoint) logger.warning(" ") logger.warning("Token: %s", token) logger.warning(" ") logger.warning("Feature URL: %s ?token= %s", url, token) logger.warning(" ") logger.warning("Query URL: %s/query?token=%s", url, token) logger.warning(" ") if args.dump: logger.warning("Dumping...") if len(sys.argv) > 2: path = sys.argv[2] else: path = 'out.xml' res = query_layer(token=token) backup_data(res['features'], path=path) if args.summary: res = query_layer(token=token) logger.warning("Summary %s entries.... ", len(res['features'])) for entry in res['features']: geo = '{0} x {1}'.format(*entry['geometry'].values()) attr = entry['attributes'] logger.warning('%s:%s @ %s', attr['OBJECTID'], attr['itemname'], geo) if args.delete: logger.warning("Deleting...") res = delete_casestudy(args.fid, token) logger.warning(res) if args.delete_all: logger.warning("Deleting all...") logger.warning(delete_all_casestudies(token)) if args.import_file: logger.warning(add_all_casestudies(args.import_file, token)) if args.edit_file: logger.warning("Editing...") fid = args.fid assert fid is not None edit_casestudy(fid, args.edit_file, token) if args.get_fid: logger.warning ("Getting OBJECTID for measureid...") measureid = args.fid # I know, fake logger.warning("OBJECTID: %s", _get_obj_OBJECTID(uid=int(measureid))) logger.warning("No arguments")