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