Skip to content

Instantly share code, notes, and snippets.

@pauldzy
Last active August 28, 2019 17:38
Show Gist options
  • Save pauldzy/c83bb77e40eb67824e01bc71ab86edcb to your computer and use it in GitHub Desktop.
Save pauldzy/c83bb77e40eb67824e01bc71ab86edcb to your computer and use it in GitHub Desktop.
ArcPy script to extract feature classes from feature and map services
import arcpy,os,http.client,json
############################################
# Edit these items per the service to scrape
host = "gispub.epa.gov";
service_path = "/arcgis/rest/services/OAR/USEPA_NEI_2005/MapServer/0"
output_fgdb = r"c:\esri_dump\fg.gdb";
output_fc = "USEPA_NEI_2005"
############################################
if not arcpy.Exists(output_fgdb):
arcpy.CreateFileGDB_management(
os.path.dirname(output_fgdb)
,os.path.basename(output_fgdb)
);
if arcpy.Exists(output_fgdb + os.sep + output_fc):
arcpy.Delete_management(output_fgdb + os.sep + output_fc);
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"};
conn = http.client.HTTPSConnection(host);
conn.request("POST",service_path,"f=json",headers);
response = conn.getresponse();
data = response.read();
json_data = json.loads(data);
if not 'currentVersion' in json_data:
raise ValueError("Error, unable to query https://" + host + service_path);
print("");
print("Extracting from " + host);
print("Service is " + service_path);
print("ArcGIS Server is version " + str(json_data['currentVersion']))
extraction_amount = json_data['maxRecordCount'];
print("Maximum record count is " + str(extraction_amount));
where = "1=1";
params = "where={}&returnIdsOnly=true&returnGeometry=false&f=json".format(where);
conn = http.client.HTTPSConnection(host);
conn.request("POST",service_path + "/query",params,headers);
response = conn.getresponse();
data = response.read();
json_data = json.loads(data);
ary_oid = sorted(json_data['objectIds']);
oid_name = json_data['objectIdFieldName'];
oid_count = len(ary_oid);
print("Total OID count = " + str(oid_count));
initial_hit = True;
counter = 0;
while counter <= oid_count - 1:
if counter + extraction_amount > oid_count - 1:
int_max = oid_count - 1;
else:
int_max = counter + extraction_amount - 1;
where = oid_name + ' >= ' + str(ary_oid[counter]) + ' AND ' + oid_name + ' <= ' + str(ary_oid[int_max]);
print(" pulling records where " + where);
fields = "*";
params = "where={}&outFields={}&returnGeometry=true&f=json".format(where, fields);
conn = http.client.HTTPSConnection(host)
conn.request("POST",service_path + "/query",params,headers)
response = conn.getresponse()
data = response.read()
json_data = json.loads(data)
ef = arcpy.AsShape(json_data,True)
if initial_hit:
arcpy.management.CopyFeatures(ef,output_fgdb + os.sep + output_fc)
initial_hit = False;
else:
arcpy.Append_management(ef,output_fgdb + os.sep + output_fc,"NO_TEST");
counter += extraction_amount;
conn.close()
del conn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment