Monday, 30 May 2016

Downloading and processing raster files in Python?




I am fairly new to python and seek guidance for a question which might sound trivial to many.


Is there a way to use 'wget' in a python script to download raster files from a server and process them in the same script?



Answer



Python has urllib2 built-in, which opens a file-pointer-like object from a IP resource (HTTP, HTTPS, FTP).


import urllib2, os

# See http://data.vancouver.ca/datacatalogue/2009facetsGridSID.htm
rast_url = 'ftp://webftp.vancouver.ca/opendata/2009sid/J01.zip'
infp = urllib2.urlopen(rast_url)


You can then transfer and write the bytes locally (i.e., download it):


# Open a new file for writing, same filename as source
rast_fname = os.path.basename(rast_url)
outfp = open(rast_fname, 'wb')

# Transfer data .. this can take a while ...
outfp.write(infp.read())
outfp.close()

print('Your file is at ' + os.path.join(os.getcwd(), rast_fname))


Now you can do whatever you want with the file.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...