Book Image

Programming ArcGIS 10.1 with Python Cookbook

By : Donald Eric Pimpler, Eric Pimpler
Book Image

Programming ArcGIS 10.1 with Python Cookbook

By: Donald Eric Pimpler, Eric Pimpler

Overview of this book

ArcGIS is an industry standard geographic information system from ESRI.This book will show you how to use the Python programming language to create geoprocessing scripts, tools, and shortcuts for the ArcGIS Desktop environment.This book will make you a more effective and efficient GIS professional by showing you how to use the Python programming language with ArcGIS Desktop to automate geoprocessing tasks, manage map documents and layers, find and fix broken data links, edit data in feature classes and tables, and much more."Programming ArcGIS 10.1 with Python Cookbook" starts by covering fundamental Python programming concepts in an ArcGIS Desktop context. Using a how-to instruction style you'll then learn how to use Python to automate common important ArcGIS geoprocessing tasks.In this book you will also cover specific ArcGIS scripting topics which will help save you time and effort when working with ArcGIS. Topics include managing map document files, automating map production and printing, finding and fixing broken data sources, creating custom geoprocessing tools, and working with feature classes and tables, among others.In "Python ArcGIS 10.1 Programming Cookbook" you'll learn how to write geoprocessing scripts using a pragmatic approach designed around an approach of accomplishing specific tasks in a Cookbook style format.
Table of Contents (21 chapters)
Programming ArcGIS 10.1 with Python Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating ZIP files


GIS often requires the use of large files that will be compressed into a .zip format for ease of sharing. Python includes a module that you can use to decompress and compress files in this format.

Getting ready

Zip is a common compression and archive format and is implemented in Python through the zipfile module. The ZipFile class can be used to create, read, and write .zip files. To create a new .zip file, simply provide the filename along with a mode such as w, which indicates that you want to write data to the file. In the following code example, we are creating a .zip file called datafile.zip. The second parameter, w, indicates that a new file will be created. A new file will be created or an existing file with the same name will be truncated in the write mode. An optional compression parameter can also be used when creating the file. This value can be set to either ZIP_STORED or ZIP_DEFLATED:

zipfile.ZipFile('dataFile.zip', 'w',zipfile.ZIP_STORED)

In this exercise, you will use Python to create file, add files, and apply compression to a .zip. You'll be archiving all the shapefiles located in the c:\ArcpyBook\data directory.

How to do it…

Follow these steps to learn how to create a script that build a .zip file:

  1. Open IDLE and create a script called c:\ArcpyBook\Appendix2\CreateZipfile.py.

  2. Import the zipfile and os modules:

    import os
    import zipfile
  3. Create a new .zip file called shapefiles.zip in write mode and add a compression parameter:

    zfile = zipfile.ZipFile("shapefiles.zip", "w", zipfile.ZIP_STORED)
  4. Next, we'll use the os.listdir() function to create a list of files in the data directory:

    files = os.listdir("c:/ArcpyBook/data")
  5. Loop through a list of all the files and write to the .zip file, if the file ends with shp, dbf, or shx:

    for f in files:
      if f.endswith("shp") or f.endswith("dbf") or f.endswith("shx"):
        zfile.write("C:/ArcpyBook/data/" + f)
  6. Print out a list of all the files that were added to the zip archive. You can use the ZipFile.namelist() function to create a list of files in the archive:

    for f in zfile.namelist():
        print "Added %s" % f
  7. Close the .zip archive:

    zfile.close()
  8. The entire script should appear as follows:

    import os
    import zipfile
    
    #create the zip file
    zfile = zipfile.ZipFile("shapefiles.zip", "w", zipfile.ZIP_STORED)
    files = os.listdir("c:/ArcpyBook/data")
    
    for f in files:
      if f.endswith("shp") or f.endswith("dbf") or f.endswith("shx"):
        zfile.write("C:/ArcpyBook/data/" + f)
    
    #list files in the archive
    for f in zfile.namelist():
        print "Added %s" % f
    
    zfile.close()
  9. Save and run the script. You should see the following output:

    Added ArcpyBook/data/Burglaries_2009.dbf
    Added ArcpyBook/data/Burglaries_2009.shp
    Added ArcpyBook/data/Burglaries_2009.shx
    Added ArcpyBook/data/Streams.dbf
    Added ArcpyBook/data/Streams.shp
    Added ArcpyBook/data/Streams.shx
    
  10. In Windows Explorer, you should be able to see the output .zip file as shown in the following screenshot. Note the size of the archive. This file was created without compression:

  11. Now, we're going to create a compressed version of the .zip file to see the difference. Make the following changes to the line of code that creates the .zip file:

    zfile = zipfile.ZipFile("shapefiles2.zip", "w", zipfile.ZIP_DEFLATED)
  12. Save and re-run the script.

  13. Take a look at the size of the new shapefiles2.zip file that you just created. Note the decreased size of the file due to compression:

How it works…

In this recipe, you created a new .zip file called shapefiles.zip in write mode. In the first iteration of this script, you didn't compress the contents of the file. However, in the second iteration, you did by using the DEFLATED parameter passed into the constructor for the ZipFile object. The script then obtained a list of files in the data directory and looped through each of the files. Each file that has an extension of .shp, .dbf, or .shx is then written to the archive file using the write() function. Finally, the names of each of the files written to the archive is printed to the screen.

There's more…

The contents of an existing file stored in a ZIP archive can be read using the read() method. The file should first be opened in a read mode, and then you can call the read() method passing in a parameter that represents the filename that should be read. The contents of the file can then be printed to the screen, written to another file, or stored as a list or dictionary variable.