Pages

Thursday, November 16, 2017

Creating MultiPoint Shapefiles with PyShp

Pyshp let's you create any type of shapefile.  Normally a point shapefile has one point per record.  But
Photo credit: VancouverMom.ca
you can also have a group of points tied to a single record in a Multipoint shapefile.  To create a Multipoint shapefile, you just use the "poly" method in the Writer. The poly method isn't just for polygons. It can also create polylines and polypoints (i.e. MultiPoints)!

Here's a simple Multipoint shapefile:


import shapefile

# Create our writer as a multipoint shapefile
w = shapefile.Writer(shapefile.MULTIPOINT)

# We'll create a single dbf field
w.field("NAME","C",40)

# Create a single-part, multi-point shape 
# by declaring the shape
# type after the parts/points list
w.poly([[[3,4],[5,6],[7,8],[9,8]]], shapeType=shapefile.MULTIPOINT)

# Create a record for this feature
w.record("Group1")

# Save the multipoint shapefile
w.save("mpoint")
Repeat the poly and record steps to add additional shapes. Add another nested list of points in the first poly method argument to add more parts to the same record

Tuesday, March 14, 2017

QGIS Python Programming Cookbook - SECOND EDITION

The second edition of the "QGIS Python Programming Cookbook" is out today from Packt Publishing!  And after my second publishing experience writing about QGIS, I can enthusiastically say it is one of the greatest open source projects, GIS software, and Python APIs out there.  It really sets a standard for quality software development.  GIS is a tool that helps people make better decisions to make the world a better place, and QGIS puts that potential in more people's hands than any other GIS software.

As with all of my books, I've tried to walk a very tricky line. I created a QGIS Python book reference book that will stand on its own if it's the only book you have.  But I also try to create content that is very different from the official documentation and book, as well as complement other third-party books out there.  QGIS is rich enough,
and updated and released with such frequency, that I don't think one book can do it justice.  While many operations in QGIS are straight-forward and easy to understand, some things can be quite complex.  For example, the Map Composer, raster operations, tinkering with various types of settings, and working with plug-ins through Python can be more difficult to understand.  And having more than one example from different sources can be invaluable in software development.  So I carefully picked examples and explanations that I felt were not easily found on the Internet for the most part, lacked documentation, or were complex enough that the over 170 examples should enrich your knowledge of QGIS regardless of you're starting from. At the very least, I hope it encourages more people to use my favorite GIS package.

Sunday, October 16, 2016

Cutting a Line Shapefile with a Point Shapefile using Shapely

Shapely works with geometric classes.  Sometimes you want those geometries to come from
shapefiles.  That's where PyShp comes in.  In this example on GIS Stack Exchange, we take a line shapefile and a point shapefile with points on or near the line segments, and split those lines at the location of the nearest point.  At a minimum, the point must be inside the envelope of the line segment:

http://gis.stackexchange.com/questions/214344/split-polyline-shapefile-using-python-with-point-shapefile/214432#214432

Wednesday, October 12, 2016

Reproject a Polygon Shapefile using PyShp and PyProj

Via the blog "Geospatiality": In this post I will use the PyShp library along with the PyProj library to
reproject the local authority boundaries of Ireland, in Shapefile format, from Irish Transverse Mercator to WGS 84 using Python.

https://glenbambrick.com/2016/01/24/reproject-shapefile/