def append_geo_json(geo_json): gis = GIS("https://www.arcgis.com", username="", password="") crime_properties = { 'title': 'Crime data', 'tags': 'crimes, open data, devlabs', 'type': 'GeoJson' } search_result = gis.content.search(query="", item_type="Feature Layer") crime_data_item = search_result[0] crime_data_feature_layer = FeatureLayer.fromitem(crime_data_item, layer_id=0) new_crime_set = FeatureSet.from_geojson(geo_json) crime_data_feature_layer.edit_features(adds=new_crime_set)
#item id of the feature layer in AGOL Organization SABER_featureLayer_item = gis.content.get('Enter Feature Layer ID Here') # In[23]: from arcgis.features import FeatureLayerCollection SABER_incidents_flayer_collection = FeatureLayerCollection.fromitem(SABER_featureLayer_item) # In[24]: from arcgis.features import FeatureLayer SABER_incidents_flayer = FeatureLayer.fromitem(SABER_featureLayer_item, layer_id=0) # In[25]: #get the count of the features in the geojson json_data = open(SABER_json_file) data2 = json.load(json_data) print(len(data2['features'])) # In[26]: #if there are features then overwrite the layer with the new data. If no features then truncate the database.
while True: if len(gis.content.search(data_name, 'Feature Layer')) > 0: locations_new_fl = gis.content.search(data_name, 'Feature Layer')[0] break print("Location Feature Layer Uploaded With ID: %s" % (locations_new_fl.id)) else: locations_new_fl = gis.content.search(data_name, 'Feature Layer')[0] print("Location Feature Layer Already Exists with ID: %s" % (locations_new_fl.id)) fl_layer = FeatureLayer.fromitem(locations_new_fl) fl_layer.manager.truncate() print("Location Data Truncated...") FeatureLayerCollection.fromitem(locations_new_fl).manager.overwrite( 'locations.csv') # Share FeatureLayer with Org locations_new_fl.share(everyone=False, org=True, groups=['CPE'], allow_members_to_edit=False) print("New Location Layer Shared with CPE Group") #Set Thumbnail of FeatureLayer locations_new_fl.update(thumbnail=r'thumbnail.jpg')
#------------------------------------------------------------------------------- # Name: arcgis truncate feature layer # Purpose: ArcGIS API truncate all records if they exist in feature layer # # Author: Ryan Nosek #------------------------------------------------------------------------------- import os, sys from arcgis.gis import GIS from arcgis.features import FeatureLayer try: gis = GIS("https://your-org.maps.arcgis.com/", "username", "pw") fl_item = gis.content.get('itemid############') fl_item_LayerID = FeatureLayer.fromitem(fl_item, layer_id=0) qry = fl_item_LayerID.query(where='1=1', returnCountOnly='true') if qry > 0: fl_item_LayerID.manager.truncate() except Exception as e: trace_back = sys.exc_info()[2] line = trace_back.tb_lineno print("Error " + str(e) + ". Near line: " + str(line))