You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-22Lines changed: 50 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Pyshp is compatible with Python 2.4-3.x.
24
24
25
25
This document provides examples for using pyshp to read and write shapefiles.
26
26
27
-
Currently the sample census blockgroup shapefile referenced in the examples is available on the google code project site at
27
+
Currently the sample census blockgroup shapefile referenced in the examples is available on the GitHub project site at
28
28
[https://github.com/GeospatialPython/pyshp](https://github.com/GeospatialPython/pyshp). These
29
29
examples are straight-forward and you can also easily run them against your
30
30
own shapefiles manually with minimal modification. Other examples for specific
@@ -46,8 +46,8 @@ Before doing anything you must import the library.
46
46
>>> import shapefile
47
47
48
48
The examples below will use a shapefile created from the U.S. Census Bureau
49
-
Blockgroups data set near San Francisco, CA and available in the subversion
50
-
repository of the pyshp google code site.
49
+
Blockgroups data set near San Francisco, CA and available in the git
50
+
repository of the pyshp GitHub site.
51
51
52
52
## Reading Shapefiles
53
53
@@ -128,13 +128,17 @@ Each shape record contains the following attributes:
128
128
'points'
129
129
'shapeType'
130
130
131
-
* shapeType: an integer representing the type of shape as defined by the shapefile specification.
131
+
* shapeType: an integer representing the type of shape as defined by the
132
+
shapefile specification.
132
133
133
134
134
135
>>> shapes[3].shapeType
135
136
5
136
137
137
-
* bbox: If the shape type contains multiple points this tuple describes the lower left (x,y) coordinate and upper right corner coordinate creating a complete box around the points. If the shapeType is a Null (shapeType == 0) then an AttributeError is raised.
138
+
* bbox: If the shape type contains multiple points this tuple describes the
139
+
lower left (x,y) coordinate and upper right corner coordinate creating a
140
+
complete box around the points. If the shapeType is a
141
+
Null (shapeType == 0) then an AttributeError is raised.
138
142
139
143
140
144
>>> # Get the bounding box of the 4th shape.
@@ -143,12 +147,16 @@ Each shape record contains the following attributes:
143
147
>>> ['%.3f' % coord for coord in bbox]
144
148
['-122.486', '37.787', '-122.446', '37.811']
145
149
146
-
* parts: Parts simply group collections of points into shapes. If the shape record has multiple parts this attribute contains the index of the first point of each part. If there is only one part then a list containing 0 is returned.
150
+
* parts: Parts simply group collections of points into shapes. If the shape
151
+
record has multiple parts this attribute contains the index of the first
152
+
point of each part. If there is only one part then a list containing 0 is
153
+
returned.
147
154
148
155
>>> shapes[3].parts
149
156
[0]
150
157
151
-
* points: The points attribute contains a list of tuples containing an (x,y) coordinate for each point in the shape.
158
+
* points: The points attribute contains a list of tuples containing an
159
+
(x,y) coordinate for each point in the shape.
152
160
153
161
>>> len(shapes[3].points)
154
162
173
@@ -183,8 +191,12 @@ You can call the "fields" attribute of the shapefile as a Python list. Each
183
191
field is a Python list with the following information:
184
192
185
193
* Field name: the name describing the data at this column index.
186
-
* Field type: the type of data at this column index. Types can be: Character, Numbers, Longs, Dates, or Memo. The "Memo" type has no meaning within a GIS and is part of the xbase spec instead.
187
-
* Field length: the length of the data found at this column index. Older GIS software may truncate this length to 8 or 11 characters for "Character" fields.
194
+
* Field type: the type of data at this column index. Types can be: Character,
195
+
Numbers, Longs, Dates, or Memo. The "Memo" type has no meaning within a
196
+
GIS and is part of the xbase spec instead.
197
+
* Field length: the length of the data found at this column index. Older GIS
198
+
software may truncate this length to 8 or 11 characters for "Character"
199
+
fields.
188
200
* Decimal length: the number of decimal places found in "Number" fields.
189
201
190
202
To see the fields for the Reader object above (sf) call the "fields"
@@ -264,7 +276,7 @@ list of field values as demonstrated in the "Reading Records" section.
264
276
Let's read the blockgroup key and the population for the 4th blockgroup:
265
277
266
278
267
-
>>> shapeRecs3.record[1:3]
279
+
>>> shapeRecs[3].record[1:3]
268
280
['060750601001', 4715]
269
281
270
282
Now let's read the first two points for that same record:
@@ -295,17 +307,18 @@ The blockgroup key and population count:
295
307
There is also an iterShapeRecords() method to iterate through large files:
296
308
297
309
>>> shapeRecs = sf.iterShapeRecords()
298
-
>>> for shape, rec in shapeRecs:
310
+
>>> for shapeRec in shapeRecs:
299
311
... # do something here
312
+
... pass
300
313
301
314
302
315
## Writing Shapefiles
303
316
304
-
The PSL tries to be as flexible as possible when writing shapefiles while
317
+
PyShp tries to be as flexible as possible when writing shapefiles while
305
318
maintaining some degree of automatic validation to make sure you don't
306
319
accidentally write an invalid file.
307
320
308
-
The PSL can write just one of the component files such as the shp or dbf file
321
+
PyShp can write just one of the component files such as the shp or dbf file
309
322
without writing the others. So in addition to being a complete shapefile
310
323
library, it can also be used as a basic dbf (xbase) library. Dbf files are a
311
324
common database format which are often useful as a standalone simple database
@@ -333,9 +346,10 @@ shapefile specification. It is important to note that numbering system has
333
346
several reserved numbers which have not been used yet therefore the numbers of
334
347
the existing shape types are not sequential.
335
348
336
-
There are three ways to set the shape type: - Set it when creating the class
337
-
instance. - Set it by assigning a value to an existing class instance. - Set
338
-
it automatically to the type of the first shape by saving the shapefile.
349
+
There are three ways to set the shape type:
350
+
* Set it when creating the class instance.
351
+
* Set it by assigning a value to an existing class instance.
352
+
* Set it automatically to the type of the first shape by saving the shapefile.
339
353
340
354
To manually set the shape type for a Writer object when creating the Writer:
341
355
@@ -443,7 +457,8 @@ Creating attributes involves two steps. Step 1 is to create fields to contain
443
457
attribute values and step 2 is to populate the fields with values for each
444
458
shape record.
445
459
446
-
The following attempts to create a complete shapefile. The attribute and field names are not very creative:
460
+
The following attempts to create a complete shapefile. The attribute and
461
+
field names are not very creative:
447
462
448
463
449
464
>>> w = shapefile.Writer(shapefile.POINT)
@@ -523,7 +538,10 @@ write them.
523
538
## Editing Shapefiles
524
539
525
540
The Editor class attempts to make changing existing shapefiles easier by
526
-
handling the reading and writing details behind the scenes. This class is experimental and should be avoided for production use. You can do the same thing by reading a shapefile into memory, making changes to the python objects, and write out a new shapefile with the same or different name.
541
+
handling the reading and writing details behind the scenes. This class is
542
+
experimental and should be avoided for production use. You can do the same
543
+
thing by reading a shapefile into memory, making changes to the python objects,
544
+
and write out a new shapefile with the same or different name.
527
545
528
546
Let's add shapes to existing shapefiles:
529
547
@@ -566,17 +584,27 @@ Remove the last shape in the polygon shapefile.
566
584
>>> e.delete(-1)
567
585
>>> e.save('shapefiles/test/polygon')
568
586
569
-
## Python __geo_interface__
587
+
## Python \_\_geo_interface\_\_
570
588
571
-
The Python __geo_interface__ convention provides a data interchange interface
589
+
The Python \_\_geo_interface\_\_ convention provides a data interchange interface
572
590
among geospatial Python libraries. The interface returns data as GeoJSON. More
573
-
information on the __geo_interface__ protocol can be found at: [https://gist.g
591
+
information on the \_\_geo_interface\_\_ protocol can be found at: [https://gist.g
574
592
ithub.com/sgillies/2217756](https://gist.github.com/sgillies/2217756). More
575
593
information on GeoJSON is available at
576
-
[http://geojson.org](http://geojson.org)
577
594
[http://geojson.org](http://geojson.org).
578
595
579
596
580
597
>>> s = sf.shape(0)
581
598
>>> s.__geo_interface__["type"]
582
599
'MultiPolygon'
600
+
601
+
# Testing
602
+
603
+
The testing framework is doctest, which are located in this file README.md.
604
+
In the same folder as README.md and shapefile.py, from the command line run
605
+
```
606
+
$ python shapefile.py
607
+
```
608
+
609
+
Linux/Mac and similar platforms will need to run `$ dos2unix README.md` in order
0 commit comments