Skip to content

Instantly share code, notes, and snippets.

@drwelby
Last active June 2, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drwelby/b88fc379230c8eac2581cb178125dd40 to your computer and use it in GitHub Desktop.
Save drwelby/b88fc379230c8eac2581cb178125dd40 to your computer and use it in GitHub Desktop.
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