2012/01/25

python tips: create shapefile with python


Long time has been passed since last post.

Here, I want to show the way to create Shapefile, which is standard data format for geometry data, using python.

Among several python tools such as geoscript, pyshp and pyproj which support operations of Shapefiles, I introduce how to use pyshp.

Pyshp is open source, provides shapefile library written all in pure python and distributed under MIT lisence.

You can download it as shapfile.py from  the official web site or just install via easy_install command:


# easy_install pyshp

However most of available operations of pyshp are introduced in the web site, I write some code using pyshp here just for my memo.

If you want to know more, I recommend looking over the web site and make some sample code according to tutorials.

#!/usr/bin/env python

import shapefile

def create_square(x=0., y=0., i=1.):
    return [[[x, y], [x+i, y], [x+i, y+i], [x, y+i], [x, y]]]

def define_geometry(w):
    # 1st record's geometry
    w.poly(parts=create_square(0, 0, 1))
    # 2nd record's geometry
    w.poly(parts=create_square(1, 1, 1))
    # 3rd record's geometry
    w.poly(parts=create_square(0.5, 0.5, 1))
    return w

def define_fields(w):
    w.field('FLD_1st')
    w.field('FLD_2nd', 'N', '10')
    return w

def fill_attributes(w):
    w.record('hoge', 'hogehogeho')
    w.record('mage', 'mage1')
    w.record('moge', 'mogemoge')
    return w

def create_proj_file(filename):
    # this function creates .proj file which defines projection.
    prj = open("%s.prj" % filename, "w")
    epsg = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
    prj.write(epsg)
    prj.close()

def main():
    filename = 'test'
    w = shapefile.Writer(shapefile.POLYGON)
    w = define_geometry(w)
    w = define_fields(w)
    w = fill_attributes(w)
    w.save('./' + filename)
    create_proj_file(filename)

if __name__ == "__main__":
    main()                                                                                             

No comments:

Post a Comment

100