Introduction to Common Tools for Spatial Information Processing Part1

Mondo Technology Updated on 2024-03-07

Geospatial datasets are often made up of multiple files, which are often published in file formats such as zip or tar. The tar format does not contain a compression algorithm, it provides an optional GZIP compression option.

Python contains modules that specifically read and write in zip and tar formats. These are the zipfile and tarfile modules, respectively.

The following example will extract a zip file hancockzip, which is hancockshp、 hancock.SHX and HancockThe dbf file will be extracted.

import zipfile

zip = open("hancock.zip", "rb")

zipshape = zipfile.zipfile(zip)

for filename in zipshape.namelist():

# print(filename)

out = open(filename, "wb")

out.write(zipshape.read(filename))

out.close()

Let's create a tar file based on the above unzipped file. In this example, open a tar file and write data to it. The write mode used at the time of writing is w:gz, which indicates that the compression format gzip is used. Related ** is as follows.

import tarfile

tar = tarfile.open("hancock.tar.gz", "w:gz")

tar.add("hancock.shp")

tar.add("hancock.shx")

tar.add("hancock.dbf")

tar.close()

print('tar.The gz file has been generated')

You can use tarfileextractall() method to extract these files. First, use a tarfileopen() method to open the file, and then extract the file, the relevant ** is as follows:

tar = tarfile.open("hancock.tar.gz", "r:gz")

allfile = tar.extractall()

tar.close()

print(allfile)

Gather knowledge to nourish you and me.

Related Pages