Last active
June 2, 2017 15:11
-
-
Save drwelby/b88fc379230c8eac2581cb178125dd40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import tempfile | |
from zipfile import Zipfile | |
import os | |
import shutil | |
def web_import(url, target): | |
''' Takes a url, downloads the file, and copies it to a feature class | |
For zipped shapefiles at the moment!! | |
Expects the target to be consistent since it truncate/appends''' | |
tfile = tempfile.NamedTemporaryFile() | |
opener = urllib2.urlopen(url) | |
tfile.write(opener.read()) | |
tempdir = tempfile.mkdtemp() | |
zfile = ZipFile(tfile) | |
zfile.extractall(tempdir) | |
tfile.close() | |
# find the shp | |
filename = None | |
for filename in os.listdir(tempdir): | |
if filename.endswith('shp'): | |
break | |
filepath = os.path.join(tempdir, filename) | |
if not arcpy.Exists(target): | |
arcpy.CopyFeatures_management(filepath, target) | |
else: | |
arcpy.TruncateTable_management(target) | |
arcpy.Append_management(filepath, target) | |
shutil.rmtree(tempdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment