Giter VIP home page Giter VIP logo

pyshp's Introduction

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.9: http://docutils.sourceforge.net/" />
<title>PyShp</title>
<meta name="author" content="Joel Lawhead - jlawhead&#64;geospatialpython.com" />

<style type="text/css">

/*
:Authors: Ian Bicking, Michael Foord
:Contact: [email protected]
:Date: 2005/08/26 
:Version: 0.1.0
:Copyright: This stylesheet has been placed in the public domain.

Stylesheet for Docutils.
Based on ``blue_box.css`` by Ian Bicking
and ``html4css1.css`` revision 1.46.
*/

@import url(html4css1.css);

body {
  font-family: Arial, sans-serif;
}

em, i {
  /* Typically serif fonts have much nicer italics */
  font-family: Times New Roman, Times, serif;
}

a.target {
  color: blue;
}

a.target {
  color: blue;
}

a.toc-backref {
  text-decoration: none;
  color: black;
}

a.toc-backref:hover {
  background-color: inherit;
}

a:hover {
  background-color: #cccccc;
}

div.attention, div.caution, div.danger, div.error, div.hint,
div.important, div.note, div.tip, div.warning {
  background-color: #cccccc;
  padding: 3px;
  width: 80%;
}

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title  {
  text-align: center;
  background-color: #999999;
  display: block;
  margin: 0;
}

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: #cc0000;
  font-family: sans-serif;
  text-align: center;
  background-color: #999999;
  display: block;
  margin: 0;
}

h1, h2, h3, h4, h5, h6 {
  font-family: Helvetica, Arial, sans-serif;
  border: thin solid black;
  /* This makes the borders rounded on Mozilla, which pleases me */
  -moz-border-radius: 8px;
  padding: 4px;
}

h1 {
  background-color: #444499;
  color: #ffffff;
  border: medium solid black;
}

h1 a.toc-backref, h2 a.toc-backref { 
  color: #ffffff;
}

h2 {
  background-color: #666666;
  color: #ffffff;
  border: medium solid black;
}

h3, h4, h5, h6 {
  background-color: #cccccc;
  color: #000000;
}

h3 a.toc-backref, h4 a.toc-backref, h5 a.toc-backref, 
h6 a.toc-backref { 
  color: #000000;
}

h1.title {
  text-align: center;
  background-color: #444499;
  color: #eeeeee;
  border: thick solid black;
  -moz-border-radius: 20px;
}

table.footnote {
  padding-left: 0.5ex;
}

table.citation {
  padding-left: 0.5ex
}

pre.literal-block, pre.doctest-block {
  border: thin black solid;
  padding: 5px;
}

.image img { border-style : solid;
            border-width : 2px;
}

h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  font-size: 100%;
}

code, tt {
  color: #000066;
}


</style>

</head>
<body>
<div class="document" id="pyshp">

<h1 class="title">PyShp</h1>

<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Joel Lawhead - <a class="last reference external" href="mailto:jlawhead&#64;geospatialpython.com">jlawhead&#64;geospatialpython.com</a></td></tr>
</tbody>
</table>

<p>:Version 1.2.0</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Revised:</th><td class="field-body">September 5, 2013</td>
</tr>
</tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#overview" id="id1">Overview</a></li>
<li><a class="reference internal" href="#examples" id="id2">Examples</a><ul>
<li><a class="reference internal" href="#reading-shapefiles" id="id3">Reading Shapefiles</a><ul>
<li><a class="reference internal" href="#reading-shapefiles-from-file-like-objects" id="id4">Reading Shapefiles from File-Like Objects</a></li>
<li><a class="reference internal" href="#reading-geometry" id="id5">Reading Geometry</a></li>
<li><a class="reference internal" href="#reading-records" id="id6">Reading Records</a></li>
<li><a class="reference internal" href="#reading-geometry-and-records-simultaneously" id="id7">Reading Geometry and Records Simultaneously</a></li>
</ul>
</li>
<li><a class="reference internal" href="#writing-shapefiles" id="id8">Writing Shapefiles</a><ul>
<li><a class="reference internal" href="#setting-the-shape-type" id="id9">Setting the Shape Type</a></li>
<li><a class="reference internal" href="#geometry-and-record-balancing" id="id10">Geometry and Record Balancing</a></li>
<li><a class="reference internal" href="#adding-geometry" id="id11">Adding Geometry</a></li>
<li><a class="reference internal" href="#creating-attributes" id="id12">Creating Attributes</a></li>
<li><a class="reference internal" href="#file-names" id="id13">File Names</a></li>
<li><a class="reference internal" href="#saving-to-file-like-objects" id="id14">Saving to File-Like Objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#editing-shapefiles" id="id15">Editing Shapefiles</a></li>
<li><a class="reference internal" href="#python-geo-interface" id="id16">Python __geo_interface__</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="overview">
<h1><a class="toc-backref" href="#id1">Overview</a></h1>
<p>PyShp provides read and write support for the Esri
Shapefile format. The Shapefile format is a popular Geographic Information
System vector data format created by Esri.  For more information about this format
please read the well-written &quot;ESRI Shapefile Technical Description - July 1998&quot;
located at <a class="reference external" href="http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf">http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf</a>.
The Esri document describes the shp and shx file formats.  However a third file
format called dbf is also required.  This format is documented on the web as the
&quot;XBase File Format Description&quot; and is a simple file-based database format created
in the 1960's.  For more on this specification see:
<a class="reference external" href="http://www.clicketyclick.dk/databases/xbase/format/index.html">http://www.clicketyclick.dk/databases/xbase/format/index.html</a></p>
<p>Both the Esri and XBase file-formats are very simple in design and
memory efficient which is part of the reason the shapefile format remains popular
despite the numerous ways to store and exchange GIS data available today.</p>
<p>Pyshp is compatible with Python 2.4-3.x.</p>
<p>This document provides examples for using PyShp to read and write shapefiles.</p>
<p>Currently the sample census blockgroup shapefile referenced in the examples is
only available on the google code project site at <a class="reference external" href="http://code.google.com/p/pyshp">http://code.google.com/p/pyshp</a>.
These examples are straight-forward and you can also easily run them against your
own shapefiles manually with minimal modification. Other examples for specific
topics are continually added to the pyshp wiki on google code and the blog
<a class="reference external" href="http://GeospatialPython.com">http://GeospatialPython.com</a>.</p>
<p>Important: For information about map projections, shapefiles,
and Python please visit: <a class="reference external" href="http://code.google.com/p/pyshp/wiki/MapProjections">http://code.google.com/p/pyshp/wiki/MapProjections</a></p>
<p>I sincerely hope this library eliminates the mundane distraction of simply
reading and writing data, and allows you to focus on the challenging and FUN
part of your geospatial project.</p>
</div>
<div class="section" id="examples">
<h1><a class="toc-backref" href="#id2">Examples</a></h1>
<p>Before doing anything you must import the library.</p>
<pre class="doctest-block">
&gt;&gt;&gt; import shapefile
</pre>
<p>The examples below will use a shapefile created from the U.S. Census Bureau
Blockgroups data set near San Francisco, CA and available in the subversion
repository of the pyshp google code site.</p>
<div class="section" id="reading-shapefiles">
<h2><a class="toc-backref" href="#id3">Reading Shapefiles</a></h2>
<p>To read a shapefile create a new &quot;Reader&quot; object and pass it the name of an
existing shapefile. The shapefile format is acutally a collection of three
files. You specify the base filename of the shapefile or the complete filename
of any of the shapefile component files.</p>
<pre class="doctest-block">
&gt;&gt;&gt; sf = shapefile.Reader(&quot;shapefiles/blockgroups&quot;)
</pre>
<p>OR</p>
<pre class="doctest-block">
&gt;&gt;&gt; sf = shapefile.Reader(&quot;shapefiles/blockgroups.shp&quot;)
</pre>
<p>OR</p>
<pre class="doctest-block">
&gt;&gt;&gt; sf = shapefile.Reader(&quot;shapefiles/blockgroups.dbf&quot;)
</pre>
<p>OR any of the other 5+ formats which are potentially part of a shapefile.
The library does not care about extensions.</p>
<div class="section" id="reading-shapefiles-from-file-like-objects">
<h3><a class="toc-backref" href="#id4">Reading Shapefiles from File-Like Objects</a></h3>
<p>You can also load shapefiles from any Python file-like object using keyword
arguments to specify any of the three files.  This feature is very powerful
and allows you to load shapefiles from a url, from a zip file, serialized
object, or in some cases a database.</p>
<pre class="doctest-block">
&gt;&gt;&gt; myshp = open(&quot;shapefiles/blockgroups.shp&quot;, &quot;rb&quot;)
&gt;&gt;&gt; mydbf = open(&quot;shapefiles/blockgroups.dbf&quot;, &quot;rb&quot;)
&gt;&gt;&gt; r = shapefile.Reader(shp=myshp, dbf=mydbf)
</pre>
<p>Notice in the examples above the shx file is never used.  The shx file is a
very simple fixed-record index for the variable length records in the shp file.
This file is optional for reading.  If it's available pyshp will use the shx file
to access shape records a little faster but will do just fine without it.</p>
</div>
<div class="section" id="reading-geometry">
<h3><a class="toc-backref" href="#id5">Reading Geometry</a></h3>
<p>A shapefile's geometry is the collection of points or shapes made from verticies
and implied arcs representing physical locations.  All types of shapefiles
just store points.  The metadata about the points determine how they are handled by
software.</p>
<p>You can get the a list of the shapefile's geometry by calling the shapes()
method.</p>
<pre class="doctest-block">
&gt;&gt;&gt; shapes = sf.shapes()
</pre>
<p>The shapes method returns a list of Shape objects describing the
geometry of each shape record.</p>
<pre class="doctest-block">
&gt;&gt;&gt; len(shapes)
663
</pre>
<p>You can iterate through the shapefile's geometry using the iterShapes() method.</p>
<pre class="doctest-block">
&gt;&gt;&gt; len(list(sf.iterShapes()))
663
</pre>
<p>Each shape record contains the following attributes:</p>
<pre class="doctest-block">
&gt;&gt;&gt; for name in dir(shapes[3]):
...     if not name.startswith('__'):
...         name
'bbox'
'parts'
'points'
'shapeType'
</pre>
<blockquote>
<ul class="simple">
<li>shapeType: an integer representing the type of shape as defined by the
shapefile specification.</li>
</ul>
</blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; shapes[3].shapeType
5
</pre>
<blockquote>
<ul class="simple">
<li>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.</li>
</ul>
</blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; # Get the bounding box of the 4th shape.
&gt;&gt;&gt; # Round coordinates to 3 decimal places
&gt;&gt;&gt; bbox = shapes[3].bbox
&gt;&gt;&gt; ['%.3f' % coord for coord in bbox]
['-122.486', '37.787', '-122.446', '37.811']
</pre>
<blockquote>
<ul class="simple">
<li>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.</li>
</ul>
</blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; shapes[3].parts
[0]
</pre>
<blockquote>
<ul class="simple">
<li>points: The points attribute contains a list of tuples containing an (x,y)
coordinate for each point in the shape.</li>
</ul>
</blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; len(shapes[3].points)
173
&gt;&gt;&gt; # Get the 8th point of the fourth shape
&gt;&gt;&gt; # Truncate coordinates to 3 decimal places
&gt;&gt;&gt; shape = shapes[3].points[7]
&gt;&gt;&gt; ['%.3f' % coord for coord in shape]
['-122.471', '37.787']
</pre>
<p>To read a single shape by calling its index use the shape() method. The index
is the shape's count from 0. So to read the 8th shape record you would
use its index which is 7.</p>
<pre class="doctest-block">
&gt;&gt;&gt; s = sf.shape(7)
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; # Read the bbox of the 8th shape to verify
&gt;&gt;&gt; # Round coordinates to 3 decimal places
&gt;&gt;&gt; ['%.3f' % coord for coord in s.bbox]
['-122.450', '37.801', '-122.442', '37.808']
</pre>
</div>
<div class="section" id="reading-records">
<h3><a class="toc-backref" href="#id6">Reading Records</a></h3>
<p>A record in a shapefile contains the attributes for each shape in the
collection of geometry. Records are stored in the dbf file. The link
between geometry and attributes is the foundation of Geographic Information
Systems. This critical link is implied by the order of shapes and
corresponding records in the shp geometry file and the dbf attribute file.</p>
<p>The field names of a shapefile are available as soon as you read a shapefile.
You can call the &quot;fields&quot; attribute of the shapefile as a Python list. Each
field is a Python list with the following information:</p>
<ul class="simple">
<li>Field name: the name describing the data at this column index.</li>
<li>Field type: the type of data at this column index. Types can be: Character, Numbers, Longs, Dates, or Memo.
The &quot;Memo&quot; type has no meaning within a GIS and is part of the xbase spec instead.</li>
<li>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 &quot;Character&quot; fields.</li>
<li>Decimal length: the number of decimal places found in &quot;Number&quot; fields.</li>
</ul>
<p>To see the fields for the Reader object above (sf) call the &quot;fields&quot; attribute:</p>
<pre class="doctest-block">
&gt;&gt;&gt; fields = sf.fields
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; assert fields == [(&quot;DeletionFlag&quot;, &quot;C&quot;, 1, 0), [&quot;AREA&quot;, &quot;N&quot;, 18, 5],
... [&quot;BKG_KEY&quot;, &quot;C&quot;, 12, 0], [&quot;POP1990&quot;, &quot;N&quot;, 9, 0], [&quot;POP90_SQMI&quot;, &quot;N&quot;, 10, 1],
... [&quot;HOUSEHOLDS&quot;, &quot;N&quot;, 9, 0],
... [&quot;MALES&quot;, &quot;N&quot;, 9, 0], [&quot;FEMALES&quot;, &quot;N&quot;, 9, 0], [&quot;WHITE&quot;, &quot;N&quot;, 9, 0],
... [&quot;BLACK&quot;, &quot;N&quot;, 8, 0], [&quot;AMERI_ES&quot;, &quot;N&quot;, 7, 0], [&quot;ASIAN_PI&quot;, &quot;N&quot;, 8, 0],
... [&quot;OTHER&quot;, &quot;N&quot;, 8, 0], [&quot;HISPANIC&quot;, &quot;N&quot;, 8, 0], [&quot;AGE_UNDER5&quot;, &quot;N&quot;, 8, 0],
... [&quot;AGE_5_17&quot;, &quot;N&quot;, 8, 0], [&quot;AGE_18_29&quot;, &quot;N&quot;, 8, 0], [&quot;AGE_30_49&quot;, &quot;N&quot;, 8, 0],
... [&quot;AGE_50_64&quot;, &quot;N&quot;, 8, 0], [&quot;AGE_65_UP&quot;, &quot;N&quot;, 8, 0],
... [&quot;NEVERMARRY&quot;, &quot;N&quot;, 8, 0], [&quot;MARRIED&quot;, &quot;N&quot;, 9, 0], [&quot;SEPARATED&quot;, &quot;N&quot;, 7, 0],
... [&quot;WIDOWED&quot;, &quot;N&quot;, 8, 0], [&quot;DIVORCED&quot;, &quot;N&quot;, 8, 0], [&quot;HSEHLD_1_M&quot;, &quot;N&quot;, 8, 0],
... [&quot;HSEHLD_1_F&quot;, &quot;N&quot;, 8, 0], [&quot;MARHH_CHD&quot;, &quot;N&quot;, 8, 0],
... [&quot;MARHH_NO_C&quot;, &quot;N&quot;, 8, 0], [&quot;MHH_CHILD&quot;, &quot;N&quot;, 7, 0],
... [&quot;FHH_CHILD&quot;, &quot;N&quot;, 7, 0], [&quot;HSE_UNITS&quot;, &quot;N&quot;, 9, 0], [&quot;VACANT&quot;, &quot;N&quot;, 7, 0],
... [&quot;OWNER_OCC&quot;, &quot;N&quot;, 8, 0], [&quot;RENTER_OCC&quot;, &quot;N&quot;, 8, 0],
... [&quot;MEDIAN_VAL&quot;, &quot;N&quot;, 7, 0], [&quot;MEDIANRENT&quot;, &quot;N&quot;, 4, 0],
... [&quot;UNITS_1DET&quot;, &quot;N&quot;, 8, 0], [&quot;UNITS_1ATT&quot;, &quot;N&quot;, 7, 0], [&quot;UNITS2&quot;, &quot;N&quot;, 7, 0],
... [&quot;UNITS3_9&quot;, &quot;N&quot;, 8, 0], [&quot;UNITS10_49&quot;, &quot;N&quot;, 8, 0],
... [&quot;UNITS50_UP&quot;, &quot;N&quot;, 8, 0], [&quot;MOBILEHOME&quot;, &quot;N&quot;, 7, 0]]
</pre>
<p>You can get a list of the shapefile's records by calling the records() method:</p>
<pre class="doctest-block">
&gt;&gt;&gt; records = sf.records()
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; len(records)
663
</pre>
<p>Similar to the geometry methods, you can iterate through dbf records using the
recordsIter() method.</p>
<pre class="doctest-block">
&gt;&gt;&gt; len(list(sf.iterRecords()))
663
</pre>
<p>Each record is a list containing an attribute corresponding to each field in the
field list.</p>
<p>For example in the 4th record of the blockgroups shapefile the 2nd and 3rd
fields are the blockgroup id and the 1990 population count of
that San Francisco blockgroup:</p>
<pre class="doctest-block">
&gt;&gt;&gt; records[3][1:3]
['060750601001', 4715]
</pre>
<p>To read a single record call the record() method with the record's index:</p>
<pre class="doctest-block">
&gt;&gt;&gt; rec = sf.record(3)
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; rec[1:3]
['060750601001', 4715]
</pre>
</div>
<div class="section" id="reading-geometry-and-records-simultaneously">
<h3><a class="toc-backref" href="#id7">Reading Geometry and Records Simultaneously</a></h3>
<p>You way want to examine both the geometry and the attributes for a record at the
same time. The shapeRecord() and shapeRecords() method let you do just that.</p>
<p>Calling the shapeRecords() method will return the geometry and attributes for
all shapes as a list of ShapeRecord objects. Each ShapeRecord instance has a
&quot;shape&quot; and &quot;record&quot; attribute. The shape attribute is a ShapeRecord object as
dicussed in the first section &quot;Reading Geometry&quot;. The record attribute is a
list of field values as demonstrated in the &quot;Reading Records&quot; section.</p>
<pre class="doctest-block">
&gt;&gt;&gt; shapeRecs = sf.shapeRecords()
</pre>
<p>Let's read the blockgroup key and the population for the 4th blockgroup:
&gt;&gt;&gt; shapeRecs[3].record[1:3]
['060750601001', 4715]</p>
<p>Now let's read the first two points for that same record:</p>
<pre class="doctest-block">
&gt;&gt;&gt; points = shapeRecs[3].shape.points[0:2]
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; len(points)
2
</pre>
<p>The shapeRec() method reads a single shape/record pair at the specified index.
To get the 4th shape record from the blockgroups shapfile use the third index:</p>
<pre class="doctest-block">
&gt;&gt;&gt; shapeRec = sf.shapeRecord(3)
</pre>
<p>The blockgroup key and population count:</p>
<pre class="doctest-block">
&gt;&gt;&gt; shapeRec.record[1:3]
['060750601001', 4715]
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; points = shapeRec.shape.points[0:2]
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; len(points)
2
</pre>
</div>
</div>
<div class="section" id="writing-shapefiles">
<h2><a class="toc-backref" href="#id8">Writing Shapefiles</a></h2>
<p>PyShp tries to be as flexible as possible when writing shapefiles while
maintaining some degree of automatic validation to make sure you don't
accidentally write an invalid file.</p>
<p>PyShp can write just one of the component files such as the shp or dbf file
without writing the others. So in addition to being a complete
shapefile library, it can also be used as a basic dbf (xbase) library. Dbf files are
a common database format which are often useful as a standalone simple
database format. And even shp files occasionaly have uses as a standalone
format. Some web-based GIS systems use an user-uploaded shp file to specify
an area of interest. Many precision agriculture chemical field sprayers also
use the shp format as a control file for the sprayer system (usually in
combination with custom database file formats).</p>
<p>To create a shapefile you add geometry and/or attributes using methods in the
Writer class until you are ready to save the file.</p>
<p>Create an instance of the Writer class to begin creating a shapefile:</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer()
</pre>
<div class="section" id="setting-the-shape-type">
<h3><a class="toc-backref" href="#id9">Setting the Shape Type</a></h3>
<p>The shape type defines the type of geometry contained in the shapefile. All of
the shapes must match the shape type setting.</p>
<p>Shape types are represented by numbers between 0 and 31 as defined by the
shapefile specification. It is important to note that numbering system has
several reserved numbers which have not been used yet therefore the numbers of
the existing shape types are not sequential.</p>
<p>You can reference shape types by the numbers or by constants defined by PyShp:
shapefile.NULL = 0
shapefile.POINT = 1
shapefile.POLYLINE = 3
shapefile.POLYGON = 5
shapefile.MULTIPOINT = 8
shapefile.POINTZ = 11
shapefile.POLYLINEZ = 13
shapefile.POLYGONZ = 15
shapefile.MULTIPOINTZ = 18
shapefile.POINTM = 21
shapefile.POLYLINEM = 23
shapefile.POLYGONM = 25
shapefile.MULTIPOINTM = 28
shapefile.MULTIPATCH = 31</p>
<p>There are three ways to set the shape type:
- Set it when creating the class instance.
- Set it by assigning a value to an existing class instance.
- Set it automatically to the type of the first shape by saving the shapefile.</p>
<p>To manually set the shape type for a Writer object when creating the Writer:</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapeType=1)
</pre>
<p>or we can use the constants as explained above:</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POINT)
</pre>
<p>As you can see, specifying the shapeType argument explicitly isn't necessary.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w.shapeType
1
</pre>
<p>OR you can set it after the Writer is created by changing the property:</p>
<pre class="doctest-block">
&gt;&gt;&gt; w.shapeType = 3
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.shapeType
3
</pre>
</div>
<div class="section" id="geometry-and-record-balancing">
<h3><a class="toc-backref" href="#id10">Geometry and Record Balancing</a></h3>
<p>Because every shape must have a corresponding record it is critical that the
number of records equals the number of shapes to create a valid shapefile. To
help prevent accidental misalignment PyShp has an &quot;auto balance&quot; feature to
make sure when you add either a shape or a record the two sides of the
equation line up. This feature is NOT turned on by default. To activate it
set the attribute autoBalance to 1 (True):</p>
<pre class="doctest-block">
&gt;&gt;&gt; w.autoBalance = 1
</pre>
<p>You also have the option of manually calling the balance() method each time you
add a shape or a record to ensure the other side is up to date.  When balancing
is used null shapes are created on the geometry side or a record with a value of
&quot;NULL&quot; for each field is created on the attribute side.</p>
<p>The balancing option gives you flexibility in how you build the shapefile.</p>
<p>Without auto balancing you can add geometry or records at anytime. You can
create all of the shapes and then create all of the records or vice versa. You
can use the balance method after creating a shape or record each time and make
updates later. If you do not use the balance method and forget to manually
balance the geometry and attributes the shapefile will be viewed as corrupt by
most shapefile software.</p>
<p>With auto balanacing you can add either shapes or geometry and update blank
entries on either side as needed. Even if you forget to update an entry the
shapefile will still be valid and handled correctly by most shapefile software.</p>
</div>
<div class="section" id="adding-geometry">
<h3><a class="toc-backref" href="#id11">Adding Geometry</a></h3>
<p>Geometry is added using one of three methods: &quot;null&quot;, &quot;point&quot;, or &quot;poly&quot;. The &quot;null&quot;
method is used for null shapes, &quot;point&quot; is used for point shapes, and &quot;poly&quot; is
used for everything else.</p>
<p><strong>Adding a Null shape</strong></p>
<p>Because Null shape types (shape type 0) have no geometry the &quot;null&quot; method is
called without any arguments.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer()
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.null()
</pre>
<p>The writer object's shapes list will now have one null shape:</p>
<pre class="doctest-block">
&gt;&gt;&gt; assert w.shapes()[0].shapeType == shapefile.NULL
</pre>
<p><strong>Adding a Point shape</strong></p>
<p>Point shapes are added using the &quot;point&quot; method. A point is specified by an
x, y, and optional z (elevation) and m (measure) value.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POINT)
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.point(122, 37) # No elevation or measure values
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.shapes()[0].points
[[122, 37, 0, 0]]
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.point(118, 36, 4, 8)
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.shapes()[1].points
[[118, 36, 4, 8]]
</pre>
<p><strong>Adding a Poly Shape</strong></p>
<p>&quot;Poly&quot; shapes can be either polygons or lines.  Shapefile polygons must have at
least 4 points and the last point must be the same as the first. PyShp automatically
enforces closed polygons.
A line must have at least two points.
Because of the similarities between these two shape types they are created using
a single method called &quot;poly&quot;.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POLYGON)
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w.poly(shapeType=3, parts=[[[122,37,4,9], [117,36,3,4]], [[115,32,8,8],
... [118,20,6,4], [113,24]]])
</pre>
<p><strong>Adding a Polygon with Rings</strong></p>
<p>Polygons consist of rings which mean they are closed.  The first point and last point
of a ring must be the same. PyShp enforces ring closure if the ring is incomplete when
you add the shape.  Polygons can have inner rings which create holes.  Holes are defined
by the order of the points.  Normally points in a ring run clockwise.  If the points
run counter-clockwise then they form a hole.  If you don't order the points correctly
you'll just have overlapping polygons.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POLYGON)
&gt;&gt;&gt; outer_ring = [[10,10],[50,50],[100,10],[50,-50],[10,10]]
&gt;&gt;&gt; inner_ring = [[40,10],[50,30],[70,10],[50,-30],[40,10]]
&gt;&gt;&gt; inner_ring.reverse()
</pre>
<p>You can use the &quot;shapefile.signed_area()&quot; method to determine if a ring is clockwise
or counter-clockwise.  A value &gt;= 0 means the ring is counter-clockwise and &lt; 0 means
the ring is clockwise.  The value returned is also the area of the polygon.</p>
<pre class="doctest-block">
&gt;&gt;&gt; # Clockwise ring
... shapefile.signed_area(outer_ring)
-4500.0
&gt;&gt;&gt; # Counter-clockwise ring
... shapefile.signed_area(inner_ring)
900.0
</pre>
<p><strong>Creating 3D Polygons</strong></p>
<p>Elevation values, known as &quot;Z&quot; values allow you to create 3-dimensional shapefiles. The
z value is an extra value specified as part of a point.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapeType=shapefile.POLYGONZ)
&gt;&gt;&gt; w.poly([[[-89.0, 33, 12], [-90, 31, 11], [-91, 30, 12]]], shapeType=15)
&gt;&gt;&gt; w.field(&quot;NAME&quot;)
&gt;&gt;&gt; w.record(&quot;PolyZTest&quot;)
&gt;&gt;&gt; w.save(&quot;shapefiles/test/MyPolyZ&quot;)
</pre>
<p>The z values are stored in a seperate shape attribute.</p>
<pre class="doctest-block">
&gt;&gt;&gt; r = shapefile.Reader(&quot;shapefiles/test/MyPolyZ&quot;)
&gt;&gt;&gt; s = r.shape(0)
&gt;&gt;&gt; s.points
[[-89.0, 33.0], [-90.0, 31.0], [-91.0, 30.0], [-89.0, 33.0]]
&gt;&gt;&gt; s.z
[12.0, 11.0, 12.0, 12.0]
</pre>
</div>
<div class="section" id="creating-attributes">
<h3><a class="toc-backref" href="#id12">Creating Attributes</a></h3>
<p>Creating attributes involves two steps. Step 1 is to create fields to contain
attribute values and step 2 is to populate the fields with values for each
shape record.</p>
<p>The following attempts to create a complete shapefile:</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POINT)
&gt;&gt;&gt; w.point(1,1)
&gt;&gt;&gt; w.point(3,1)
&gt;&gt;&gt; w.point(4,3)
&gt;&gt;&gt; w.point(2,2)
&gt;&gt;&gt; w.field('FIRST_FLD')
&gt;&gt;&gt; w.field('SECOND_FLD','C','40')
&gt;&gt;&gt; w.record('First','Point')
&gt;&gt;&gt; w.record('Second','Point')
&gt;&gt;&gt; w.record('Third','Point')
&gt;&gt;&gt; w.record('Fourth','Point')
&gt;&gt;&gt; w.save('shapefiles/test/point')
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POLYGON)
&gt;&gt;&gt; w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
&gt;&gt;&gt; w.field('FIRST_FLD','C','40')
&gt;&gt;&gt; w.field('SECOND_FLD','C','40')
&gt;&gt;&gt; w.record('First','Polygon')
&gt;&gt;&gt; w.save('shapefiles/test/polygon')
</pre>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POLYLINE)
&gt;&gt;&gt; w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
&gt;&gt;&gt; w.poly(parts=[[[1,3],[5,3]]], shapeType=shapefile.POLYLINE)
&gt;&gt;&gt; w.field('FIRST_FLD','C','40')
&gt;&gt;&gt; w.field('SECOND_FLD','C','40')
&gt;&gt;&gt; w.record('First','Line')
&gt;&gt;&gt; w.record('Second','Line')
&gt;&gt;&gt; w.save('shapefiles/test/line')
</pre>
<p>You can also add attributes using keyword arguments where the keys are field names.</p>
<pre class="doctest-block">
&gt;&gt;&gt; w = shapefile.Writer(shapefile.POLYLINE)
&gt;&gt;&gt; w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
&gt;&gt;&gt; w.field('FIRST_FLD','C','40')
&gt;&gt;&gt; w.field('SECOND_FLD','C','40')
&gt;&gt;&gt; w.record(FIRST_FLD='First', SECOND_FLD='Line')
&gt;&gt;&gt; w.save('shapefiles/test/line')
</pre>
</div>
<div class="section" id="file-names">
<h3><a class="toc-backref" href="#id13">File Names</a></h3>
<p>File extensions are optional when reading or writing shapfiles.  If you specify them Pyshp
ignores them anyway. When you save files you can specify a base file name that is used for
all three file types.  Or you can specify a nmae for one or more file types.  In that case,
any file types not assigned will not save and only file types with file names will be saved.
If you do not specify any file names (i.e. save()), then a unique file name is generated with
the prefix &quot;shapefile_&quot; followed by random characters which is used for all three files.  The
unique file name is returned as a string.</p>
<pre class="doctest-block">
&gt;&gt;&gt; targetName = w.save()
&gt;&gt;&gt; assert(&quot;shapefile_&quot; in targetName)
</pre>
</div>
<div class="section" id="saving-to-file-like-objects">
<h3><a class="toc-backref" href="#id14">Saving to File-Like Objects</a></h3>
<p>Just as you can read shapefiles from python file-like objects you can also write them.</p>
<pre class="doctest-block">
&gt;&gt;&gt; try:
...     from StringIO import StringIO
... except ImportError:
...     from io import BytesIO as StringIO
&gt;&gt;&gt; shp = StringIO()
&gt;&gt;&gt; shx = StringIO()
&gt;&gt;&gt; dbf = StringIO()
&gt;&gt;&gt; w.saveShp(shp)
&gt;&gt;&gt; w.saveShx(shx)
&gt;&gt;&gt; w.saveDbf(dbf)
&gt;&gt;&gt; # Normally you would call the &quot;StringIO.getvalue()&quot; method on these objects.
&gt;&gt;&gt; shp = shx = dbf = None
</pre>
</div>
</div>
<div class="section" id="editing-shapefiles">
<h2><a class="toc-backref" href="#id15">Editing Shapefiles</a></h2>
<p>The Editor class attempts to make changing existing shapefiles easier by handling the reading and writing details behind the scenes.</p>
<p>Let's add shapes to existing shapefiles:</p>
<p>Add a point to a point shapefile</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/point.shp&quot;)
&gt;&gt;&gt; e.point(0,0,10,2)
&gt;&gt;&gt; e.record(&quot;Appended&quot;,&quot;Point&quot;)
&gt;&gt;&gt; # We added z and m values so
&gt;&gt;&gt; # change the shapetype
&gt;&gt;&gt; e.shapeType = shapefile.POINTZ
&gt;&gt;&gt; e.save('shapefiles/test/point')
</pre>
<p>Edit the appended point to change the &quot;y&quot; and &quot;z&quot; value</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/point.shp&quot;)
&gt;&gt;&gt;     # Find the point by the attribute
&gt;&gt;&gt; for s in enumerate(e.records):
...     i, record = s
...     if record[0] == &quot;Appended&quot;:
...         geom = e._shapes[i]
...         # Change the y value to 5
...         geom.points[0][1] = 5
...         # Change the z value to 9
...         if hasattr(geom, &quot;z&quot;):
...                 geom.z = (9,)
...         else:
...             geom.points[0][2] = 9
&gt;&gt;&gt; e.save('shapefiles/test/point')
</pre>
<p>Add a new line to a line shapefile:</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/line.shp&quot;)
&gt;&gt;&gt; e.line(parts=[[[10,5],[15,5],[15,1],[13,3],[11,1]]])
&gt;&gt;&gt; e.record('Appended','Line')
&gt;&gt;&gt; e.save('shapefiles/test/line')
&gt;&gt;&gt; e = None
</pre>
<p>Add a new polygon to a polygon shapefile:</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/polygon.shp&quot;)
&gt;&gt;&gt; e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]])
&gt;&gt;&gt; e.record(&quot;Appended&quot;,&quot;Polygon&quot;)
&gt;&gt;&gt; e.save('shapefiles/test/polygon')
&gt;&gt;&gt; e = None
</pre>
<p>Remove the first point in each shapefile - for a point shapefile that is
the first shape and record&quot;</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/point.shp&quot;)
&gt;&gt;&gt; e.delete(0)
&gt;&gt;&gt; e.save('shapefiles/test/point')
&gt;&gt;&gt; e = None
</pre>
<p>Remove the last shape in the polygon shapefile.</p>
<pre class="doctest-block">
&gt;&gt;&gt; e = shapefile.Editor(shapefile=&quot;shapefiles/test/polygon.shp&quot;)
&gt;&gt;&gt; e.delete(-1)
&gt;&gt;&gt; e.save('shapefiles/test/polygon')
&gt;&gt;&gt; e = None
</pre>
</div>
<div class="section" id="python-geo-interface">
<h2><a class="toc-backref" href="#id16">Python __geo_interface__</a></h2>
<p>The Python __geo_interface__ convention provides a data interchange interface
among geospatial Python libraries.  The interface returns data as GeoJSON.
More information on the __geo_interface__ protocol can be found at:
<a class="reference external" href="https://gist.github.com/sgillies/2217756">https://gist.github.com/sgillies/2217756</a>.
More information on GeoJSON is available at <a class="reference external" href="http://geojson.org">http://geojson.org</a> <a class="reference external" href="http://geojson.org">http://geojson.org</a>.</p>
<pre class="doctest-block">
&gt;&gt;&gt; s = sf.shape(0)
&gt;&gt;&gt; s.__geo_interface__[&quot;type&quot;]
'MultiPolygon'
</pre>
</div>
</div>

</div>
</body>
</html>

pyshp's People

pyshp's Issues

Correct data type should be enforced when adding point data.

What steps will reproduce the problem?
1.Add point data in a non-number format (this is not very smart when doing it 
by hand, but easy to do when parsing data from an input file):
sf.point('12.23','14.24') 

2.Attempt to save the shapefile:
sf.save('shapefile')

3.See error that doesn't make a lot of sense:
 File "../../py_libs/pyShp/shapefile.py", line 492, in __shapefileHeader
    f.write(pack("<4d", *self.bbox()))
struct.error: required argument is not a float

It would be nice if when adding a point a legible error was thrown if the data 
was in a format that would work (such as accidentally adding numbers that are 
encoded as string values from a csv file)

Original issue reported on code.google.com by [email protected] on 12 Jan 2011 at 5:30

Better error reporting for missing shx

I was using pyshp today on a set of test shapefiles.  When I copied my test 
shapefiles into the test folder, I missed one of the .shx files.  I received an 
exception when the code attempted to access one of the shapefiles.  Summarized, 
my code looked like this:

import shapefile
reader = shapefile.Reader(r'D:\work\code\test\streets.shp')
reader.shape(0)

I received the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shapefile.py", line 220, in shape
    offset = self.__shapeIndex(i)
  File "shapefile.py", line 196, in __shapeIndex
    f = self.__getFileObj(self.shx)
  File "shapefile.py", line 115, in __getFileObj
    raise ShapefileException("Required file not available.")
shapefile.ShapefileException: Required file not available.

Now, technically, there is nothing wrong with this message.  I'm dealing with 
an "exceptional case" and an Exception was thrown in this case.  I just wish it 
was a bit more specific about which file was missing.  So, I patched 
shapefile.py to report the issue more cleanly for the missing shx case.

I have attached a diff with my change.

Joseph Armbruster

Original issue reported on code.google.com by [email protected] on 30 Jun 2011 at 7:39

Attachments:

delete method of Editor class will not delete the 0th shape of the shapefile

What steps will reproduce the problem?
1. >> e = shapefile.Editor(shapefile="shapefiles/test/polygon.shp")
2. >> print "No. of shapes: %i" % len(e.shapes())
3. >> e.delete(0)
4. >> print "No. of shapes: %i" % len(e.shapes())

What is the expected output? What do you see instead?

I expect the number of shapes to be reduced by one after the delete method is 
called. Instead the number of shapes remains the same.

What version of the product are you using? On what operating system?

Using shapefile-1.1.6-b.py under Python 2.7 under linux (Fedora 14).

Please provide any additional information below.

The problem is caused by the way that the delete method determines whether 
points, shapes, or parts are to be deleted.  This logic fails because Python 
evaluates 0 shape index as "False" when it applies the filtering logic.  The 
attached patch fixes the problem.


Original issue reported on code.google.com by [email protected] on 30 Dec 2012 at 1:45

Attachments:

Topic of "projections" should be broached in the how-to-guide

Shapefiles are unprojected data.  For many users this means an extra step after 
pyshp has run.  A few words about common projections and what "good" coordinate 
data looks like will help.

I don't expect pyshp to handle projections, but guiding reads about the issue 
and offering some tips would be nice
http://trac.osgeo.org/proj/
http://wiki.openstreetmap.org/wiki/Ogr2osm

Original issue reported on code.google.com by [email protected] on 26 Aug 2011 at 8:07

No iterator method for large file sizes

What steps will reproduce the problem?
1. Download a shapefile of 100+ mb
2. Run the .shapes() method

What version of the product are you using? On what operating system?
Mac OSX Lion, pyshp 1.1.6-beta

Please provide any additional information below.

Created an iterator class within the reader class, to allow for large file 
processing. Method to use iterator is Reader.shapes_iter()

Original issue reported on code.google.com by [email protected] on 9 Jul 2012 at 5:34

Attachments:

Suggestion for pyshp_usage

Hi,

I saw the usage.py file used to get installed which was a bit of a generic 
name.  Now that been changed to pyshp_usage.py, so it's more specific which is 
good.

Installed it looks like

ls -1 e1/lib/python2.7/site-packages/

pyshp_usage.py
pyshp_usage.pyc
shapefile.py
shapefile.pyc

The pyshp_usage.py is not really used though since it is just a docstring in 
rst format that has examples run by doctest.

Suggest that pyshp_usage.py could be changed to a README or README.txt file 
with the doc in rst format.  doctest should be able to run on that file too, 
might just have to adjust a bit.

Advantage with this is that the README file will be packaged as part of the 
sdist (source distribution) but won't be installed into 
lib/python2.7/site-packages/.

Thoughts?

:)

Original issue reported on code.google.com by [email protected] on 24 Sep 2011 at 2:35

dbfHeader patch

What steps will reproduce the problem?
1. Use shapefile.py for parse my shapes.
2. Got an error.
3. Make a small fix in shapefile.py
4. Profit :)

What is the expected output? What do you see instead?
I see
Traceback (most recent call last):
...
  File "shp2sql.py", line 99, in __init__
    self.sfo = VShapeRecords(shpFileName)
  File "shp2sql.py", line 21, in __init__
    self.sfo = shapefile.Reader(filename)
  File "c:\d\code\shape\shapefile.py", line 88, in __init__
    self.load(shapefile)
  File "c:\d\code\shape\shapefile.py", line 108, in load
    self.__dbfHeader()
  File "c:\d\code\shape\shapefile.py", line 250, in __dbfHeader
    fieldDesc[name] = fieldDesc[name][:fieldDesc[name].index("\x00")]
ValueError: substring not found


What version of the product are you using? On what operating system?
Last from CVS.

Please provide any additional information below.

If in file shapefile.py in method
def __dbfHeader(self):
change line (250)
from
fieldDesc[name] = fieldDesc[name][:fieldDesc[name].index("\x00")]
to
fieldDesc[name] = fieldDesc[name].replace('\0', '')

all works fine for me.
Sample shape in attach.

Original issue reported on code.google.com by [email protected] on 3 Apr 2011 at 11:07

Attachments:

Only every 5th shape is valid.

What steps will reproduce the problem?
1. Unzip attached file
2. Run:

from shapefile import Reader)
rdr = Reader('dryclean_sp_vw')
for s in rdr.shapes():
   print s.shapeType, s.points

What is the expected output? What do you see instead?

Expect to see valid shapeType and a single point on each output line.

Actually see:

21          [[526856.2550000001, 426634.1260000001]]
1092619666  []
50331648    []
-2104533974 []
1092198249  []
21          [[524895.9480000002, 428961.89000000013]]
1092603877  []
100663296   []
498216209   []
1092281351  []

What version of the product are you using? On what operating system?

V 1.1.4 on Windows 7 w/ python 2.7.

Original issue reported on code.google.com by [email protected] on 27 Apr 2012 at 8:13

Attachments:

Error Reading PointZ type records

PointZ records are not read in properly. To fix this line 188 in shapefile.py 
should read

185     if shapeType == 11:
186         record.z = unpack("<d", f.read(8))
187     # Read a single M value
188     if shapeType == 11 or shapeType == 21:
189         record.m = unpack("<d", f.read(8))

This is documented on page 15 of the ESRI white paper.

http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf


Original issue reported on code.google.com by [email protected] on 18 Apr 2011 at 6:42

Shapefile with other encoding than UTF-8 fails

Instead of loading the shapefile, an error arises:
Traceback (most recent call last):
  File "/[path]/import_shapefile.py", line 115, in <module>
    loadShapefile()
  File "/[path]/import_shapefile.py", line 19, in loadShapefile
    records = sf.shapeRecords()
  File "/[path]/shapefile.py", line 430, in shapeRecords
    for rec in zip(self.shapes(), self.records())]
  File "/[path]/shapefile.py", line 413, in records
    r = self.__record()
  File "/[path]/shapefile.py", line 389, in __record
    value = u(value)
  File "/[path]/shapefile.py", line 53, in u
    return v.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc6 in position 1: invalid 
continuation byte


Using:
pyshp version: 1.1.4
Ubuntu 12.04
Python 3.2.3 from Ubuntu 12.04 repos

For me it works using the following patch:
==============
@@ -50,7 +50,7 @@
     if PYTHON3:
         if isinstance(v, bytes):
             # For python 3 decode bytes to str.
-            return v.decode('utf-8')
+            return v.decode('iso-8859-1')
         elif isinstance(v, str):
             # Already str.
             return v
==============

A better solution, I think, would be to make it possible for the user to define 
custom encoding when loading shapefile using arguments.

Original issue reported on code.google.com by [email protected] on 29 Jun 2012 at 8:04

Holes in polygon?

Are island polygons/polygons with holes supported? If so, please add an example 
to the wiki. I've been trying to create such with pyshp, but didn't manage.

Original issue reported on code.google.com by [email protected] on 21 Apr 2012 at 11:15

Date Field Type Improvement

Handling date fields (type 'D') is currently done awkwardly and incorrectly in 
pyshp.

Try writing a shapefile with a 'D' type field, re-opening it for with an 
`Editor`, and saving the file: the date field becomes corrupted because pyshp 
has written it's value as a string version of [2012,02,18], which is "[2012,02".

Issue 6 <http://code.google.com/p/pyshp/issues/detail?id=6&can=1> suggested 
regex parsing of date fields, falling back to parsing out positions, but the 
fact is that this is unnecessary. The DBF format 'D' field must be exactly 8 
characters, with date stored as YYYYMMDD; no delimiters, no ambiguity with size.

I've attached a patch (unified diff format) against current trunk (r72) which 
does the following:
* overrides column size when 'D' type is specified so that it is always exactly 
8 characters
* marshalls to and from the actual Python datetime.date type; no string 
formatting or arrays-of-integers for dealing with dates

Original issue reported on code.google.com by [email protected] on 25 Feb 2012 at 10:29

Attachments:

shapefile.py does not generate projection file (.prj)

Here's my code:

import shapefile as sf
w = sf.Writer(sf.POINT)
w.point(37.7793, -122.4192)
w.field('FIRST_FLD')
w.record('First','Point')
w.save('test/point')

...it creates point.dbf, point.shp, and point.shx, but it does not create 
point.prj

Since the projection file is not generated I cannot open it in Google Earth Pro

Original issue reported on code.google.com by [email protected] on 10 Feb 2011 at 11:23

ValueError: invalid literal for int() with base 10: ''

The parcel/point shapefile from http://www.ccmap.us/catalog.asp gives:

>>> sf = shapefile.Reader("CAD_AO_ParcelPoint_0711")
>>> sf.fields
[('DeletionFlag', 'C', 1, 0), ['APN', 'C', 9, 0], ['PARC_PY_ID', 'N', 19, 11], 
['REV_DATE', 'D', 8, 0], ['S_STR_NM', 'C', 30, 0], ['S_STR_SUF', 'C', 4, 0], 
['S_STR_NBR', 'C', 5, 0], ['S_FRAC', 'C', 4, 0], ['S_APT_NBR', 'C', 9, 0], 
['S_CTY_ABBR', 'C', 5, 0], ['S_ZIP', 'C', 5, 0], ['S_ZIP_EXT', 'C', 4, 0]]
>>> shapes = sf.shapes()
>>> len(shapes)
370097
>>> records = sf.records()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shapefile.py", line 315, in records
    r = self.__record()
  File "shapefile.py", line 286, in __record
    y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
ValueError: invalid literal for int() with base 10: ''

Original issue reported on code.google.com by [email protected] on 26 Aug 2011 at 8:07

pyshp will not notify you if your .dbf file is incomplete

What steps will reproduce the problem?
1. Unzip the attached zip file.
2. reader = Reader('bedrock.shp')
3. reader.shape(2)
4. reader.record(2) => generates error

What is the expected output? What do you see instead?
The attached shapefile is missing the last record in the .dbf.  I think the 
code should compare the actual file size against the expected file size
using fields in the .dbf header (fs = headerlen + nrecords*recordlen).  If they 
don't match, throw an exception from the __init__() method.

What version of the product are you using? On what operating system?
1.1.4, on Mac OS 10.7.5

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 6 Feb 2013 at 12:22

Attachments:

Polygons cannot be built using tuple points

If the Python `zip` function is used to combine X and Y coordinates, the result 
is a list of tuples:

X = [1, 5, 5, 3, 1]
Y = [5, 5, 1, 3, 1]
coors = zip(X, Y) # list of tuples


However, this object cannot be used by the shapefile.py module:

import shapefile
w = shapefile.Writer(shapefile.POLYGON)
w.field('MYFIELD','C')
w.poly([coors])
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "shapefile.py", line 684, in poly
    point.append(0)
AttributeError: 'tuple' object has no attribute 'append'


To fix this, I've added a check to shapefile.py near line 679:
# Ensure point is list
if not isinstance(point, list):
        point = list(point)

See attached patch. I've also removed trailing whitespace on various lines in 
this patch.

Original issue reported on code.google.com by [email protected] on 3 Mar 2011 at 12:04

Attachments:

OverflowError: Python int too large to convert to C long

What steps will reproduce the problem?
1. Install attached file.
2. Run

from shapefile import Reader
rdr = Reader('brownfield_sites')
for s in rdr.shapes():
   print s.shapeType

What is the expected output? What do you see instead?

Expect to see list of shapeType constants, instead see exception:

Traceback (most recent call last):
  File "LoadGISFeatures.py", line 27, in <module>
    load_file('X:/IT/Florida/brownfield_sites/brownfield_sites')
  File "LoadGISFeatures.py", line 20, in load_file
    for shape in rdr.shapes()[:10]:
  File "C:\Python27\lib\site-packages\pyshp-1.1.4-py2.7.egg\shapefile.py", line
310, in shapes
    shapes.append(self.__shape())
  File "C:\Python27\lib\site-packages\pyshp-1.1.4-py2.7.egg\shapefile.py", line
239, in __shape
    record.parts = _Array('i', unpack("<%si" % nParts, f.read(nParts * 4)))
OverflowError: Python int too large to convert to C long

What version of the product are you using? On what operating system?

v 1.1.4 on Windows 7 w/ Python 2.7

Original issue reported on code.google.com by [email protected] on 27 Apr 2012 at 8:17

Attachments:

Invalid number when reading shape records

What steps will reproduce the problem?
1. Load the attached file
2. Ask for shapeRecords()

Get this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shapefile.py", line 430, in shapeRecords
    for rec in zip(self.shapes(), self.records())]
  File "shapefile.py", line 413, in records
    r = self.__record()
  File "shapefile.py", line 378, in __record
    value = int(value)
ValueError: invalid literal for int() with base 10: 
'********************************'



Original issue reported on code.google.com by 127.0.0.69 on 21 Mar 2012 at 4:03

Attachments:

Fields with garbage after NULL are read back as strings

If you have a DBF field that has garbage after the terminating NULL, the record 
will contain the terminating NULL and the garbage, even if it is defined as 
something else than C (F for instance).

The expected behavior is to end the string at the NULL, disregard anything 
after the NULL and do the correct conversion based on field type.

Original issue reported on code.google.com by [email protected] on 2 May 2013 at 4:45

Deleting all shapes

What steps will reproduce the problem?
try to save an empty shape

w = shapefile.Writer(shapefile.POLYGON)
w.field('ID','N','16')
w.field('Name','C','40')
w.save('polygon.shp')


What is the expected output? What do you see instead?
get an empty shapefile
get an error because pyshp is trying to calculate the bbox of nothing

File "shapefile.py", line 529, in __bbox
    return [min(x), min(y), max(x), max(y)]
ValueError: min() arg is an empty sequence

What version of the product are you using? On what operating system?
Windows Vista SP2
Python 2.6.5
Pyshp 1.1.4

Original issue reported on code.google.com by [email protected] on 7 Dec 2011 at 3:32

Cannot save POINTZ file

With Shapefile r28 (for Python2.x), I cannot seem to write a simple 3D 
Shapefile:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import shapefile
>>> sf = shapefile.Writer(shapefile.POINTZ)
>>> sf.field('RecID')
>>> sf.point(100.0, 200.0, 300.0)
>>> sf.record(1)
>>> sf.save('foo.shp')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "I:\somepath\shapefile.py", line 765, in save
    self.saveShp(target)
  File "I:\somepath\shapefile.py", line 738, in saveShp
    self.__shapefileHeader(self.shp, headerType='shp')
  File "I:\somepath\shapefile.py", line 486, in __shapefileHeader
    f.write(pack(">i", self.__shpFileLength()))
  File "I:\somepath\shapefile.py", line 372, in __shpFileLength
    nParts = len(s.parts)
AttributeError: _Shape instance has no attribute 'parts'

Original issue reported on code.google.com by [email protected] on 21 Apr 2011 at 5:02

Fail to detect shapetype

This code:
import shapefile
sf = shapefile.Editor('/home/jorge/temp/42MUE250GC_SIR.shp')
shapes = sf.shapes()
for s in shapes:
    print( s.shapeType)
    for p in range(1):#len(s.points)):
        s.points[p][0] = -s.points[p][0]
sf.save('/home/jorge/temp/x.shp')

give all shapetype = 25 and a error:
"  File 
"/usr/local/lib/python2.7/site-packages/pyshp-1.1.4-py2.7.egg/shapefile.py", 
line 687, in __shpRecords
    [f.write(pack("<d", p[3])) for p in s.points]
IndexError: array index out of range
"
But my shapefile have only 2D objects, there is no exists p[2] or p[3]

Another code, using gdal/osgeo that read the same shapefile, give me that most 
objects are POLYGON' and a few are MULTIPOLYGON

What is the expected output? What do you see instead?
Why the two libraries have different results for the geometry type for the same 
file?



What version of the product are you using? On what operating system?
pyshape 1.1.4 and openSUSE 12.2 (x86_64)


Please provide any additional information below.
This code is a simple test. I'm trying to convert a 2D file in a 3D file.
I receive a shapefile of highways and need to add the elevation data to all 
points.


Original issue reported on code.google.com by [email protected] on 20 Mar 2013 at 9:18

Missing record fields

What steps will reproduce the problem?
1. Download the dataset from 
http://www1.toronto.ca/wps/portal/open_data/open_data_item_details?vgnextoid=9ec
d5f9cd70bb210VgnVCM1000003dd60f89RCRD&vgnextchannel=6e886aa8cc819210VgnVCM100000
67d60f89RCRD
2. Run this sample program to print all of the records

import shapefile
sf = shapefile.Reader("TCL3_Centreline_OD_bikeways")
shapes = sf.shapes()
records = sf.records()

for index, i in enumerate(shapes):
    print records[index]

3. search through the CP_TYPE column for data. I grepped the output of my 
python program for the work bike
python test.py | grep -i "bike"

What is the expected output? What do you see instead?
The CP_TYPE column should have data like "Bike Lanes", "Signed Routes", 
"Suggested On-Street Routes", ... but it is always empty

What version of the product are you using? On what operating system?
OSX Snow Leopard 
Python 2.6.1
pyshp 1.0.5
Bikeway data was updated March 2011

I get the expected output using other code like this guys 
http://indiemaps.com/blog/2008/03/easy-shapefile-loading-in-python/

Original issue reported on code.google.com by [email protected] on 3 Sep 2011 at 4:55

Reader fails for unicode strings

What steps will reproduce the problem?
1. sf = shapefile.Reader(u'C:\\Test\\MyFile.shp')

What is the expected output? What do you see instead?

Expected to get a new Reader class. Instead get:

ShapefileException("Shapefile Reader requires a shapefile or file-like object.")

What version of the product are you using? On what operating system?

version: 1.1.4
Seems to be the same code in the trunk. 

Please provide any additional information below.

It fails due to the type checking in the __init__ function of the reader, as a 
string and unicode strings are different types. 
Is type checking necessary at all here?

if type(args[0]) is type("stringTest"): 

>>> type("hello")
<type 'str'>
>>> type(u"hello")
<type 'unicode'>

Original issue reported on code.google.com by [email protected] on 21 Nov 2012 at 9:05

Adding keyed dictionary for a record in Writer does not work

What steps will reproduce the problem?
1.Created keyed dictionary 
sfRecord = {"num1":"bob","num2":"bob2","num3":"bob3"}
2.Add it to the writer
w.field("field1")
w.field("field2")
w.field("field3")
w.record(recordDict=sfRecord)

What is the expected output? What do you see instead?
print sf.records
[]

You should see something.
The issue is in def record(self, *recordList, **recordDict):
...
elif recordDict:
                                for field in self.fields:
                                        if recordDict.get(field[0], None):
                                                record.append(recordDict[field[0]])
Here, recordDict is a keyed object with 'recordDict' as the only keyed object 
in it.
So, replacing recordDict above with recordDict['recordDict'] will get you an 
output.

However, this will still cause an issue where if the key is present but the 
value isn't, the value isn't appended. This causes the later fields to shift.
So an input of this:

sfRecord = {"num1":"bob","num2":"","num3":"bob3"}

will result in an array in record like this:

["bob","bob3"], which is wrong.

Instead we could rewrite the whole block to look something like this (hopefully 
a little more elegant with the names):

recordDict=recordDict['recordDict']
for field in self.fields:
    if field[0] in recordDict:
        record.append(recordDict[field[0]])


Original issue reported on code.google.com by [email protected] on 11 Jan 2011 at 6:36

Use python's standard packaging

Hi,

It's important to make python packages easily installable for users.  I saw one 
comment on the wiki about this and I certainly want to be able to pip install 
or easy_install this package.  It's a real pain when you see you have to 
download a file manually and put it somewhere manually.

Attached is a setup.py file that works with standard python packaging so it can 
be on pypi with easy installation.

Simply create a ~/.pypirc file with:

[distutils]
index-servers =
    pypi

[pypi]
username:yourusername
password:yourpassword

Then in your project dir you can register the pyshp project which only ever 
needs to be done once with:

python setup.py register

From then on when creating a new release you can create a source tarball with:

python setup.py sdist

And once you are happy you can upload with:

python setup.py sdist upload


I tested all these and they work which made pyshp installable with:

pip install pyshp

easy_install pyshp

(I have since deleted the package that I registered and uploaded for testing).


You can also use develop mode when working on the project with:

python setup.py develop

Original issue reported on code.google.com by [email protected] on 31 Aug 2011 at 5:08

Attachments:

load / save file like object support in 1 line

Support was added to save to file like objs recently.

This is good, but I need to load not save from file like objs (they are 
compressed, but could also come from network - no file).

It would be nice to have load / save consistent and be simple 1 line.

Suggested solution in attached patch along with doctests for them.

Original issue reported on code.google.com by [email protected] on 18 Sep 2011 at 7:06

Attachments:

DBF file with numeric overflow in value

What steps will reproduce the problem?
1. Open a shapefile in which its dbf has an overflow in a column (such as [1])
2. Read records

What is the expected output? What do you see instead?
It shouldn't crash, it crashes because it cannot convert asterisks to numbers.

What version of the product are you using? On what operating system?
Trunk

Please provide any additional information below.
According to [2] DBf files can contain numeric fields with asterisks. I suppose 
the best way to handle them is as if they where null. Right now pyshp converts 
those to 0. Attached is a patch that handles this issue.

[1] 
http://intgis.montevideo.gub.uy/sit/php/common/datos/generar_zip2.php?nom_tab=v_
uptu_lsv_destinos&tipo=gis
[2] 
http://connect.microsoft.com/SQLServer/feedback/details/648410/sql-reports-an-er
ror-when-querying-standard-dbf-data-via-vfp-ole-db-provider

Original issue reported on code.google.com by [email protected] on 1 May 2013 at 4:01

Attachments:

Indentation errors, whitespace, spelling

Checking syntax of files using (e.g. in the Python3 directory):
> python -c "import shapefile3"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "shapefile3.py", line 772
    def __init__(self, shapefile=None, shapeType=POINT, autoBalance=1):
                                                                      ^

I've fixed up the indentation to consistently use tabs. I've also removed any 
extra whitespace and the end of lines, and corrected some spelling mistakes.

Original issue reported on code.google.com by [email protected] on 5 Mar 2011 at 7:29

Attachments:

Deleting Shapes

Deleting of the first line does not work. (nothing happend)

e = shapefile.Editor(shapefile="point")
e.delete(0)
e.save('point')


When deleting a line between first and last line, the shape is deleted, but the 
record is still there and so all records are shifted

e = shapefile.Editor(shapefile="point")
e.delete(1)
e.save('point')

( the old shape 2 has attributes of the old shape 1)


Environment:
Windows Vista SP2
Python 2.6.5
Pyshp 1.1.4

Original issue reported on code.google.com by [email protected] on 7 Dec 2011 at 2:38

Suggestion for python 2 / 3

Hi,

With both python 2 / 3 branches separate they have to be maintained and get out 
of sync with the duplication.

Suggest keeping the 1 codebase and make it work with both python 2 and python 
3.  This is becoming quite a common approach and if it is suitable for pyshp 
then it means only 1 codebase and also only 1 package on pypi that works for 
all python versions.

I may be able to help with this if there is interest.

Thoughts?

:)

Original issue reported on code.google.com by [email protected] on 24 Sep 2011 at 2:39

I made some pyshp edits to allow a list for point coordinates and a list or dict for records

This isn't a bug report.  The pyshp library is awesome!  I actually just have a 
few suggestions for the pyshp code.  

The first is to allow a list of coordinates when writing points:

Currently you must do this:

    w = shapefile.Writer(shapefile.POINTZ)
    coordList = [-80, 40, 300]
    x = coordList[0]
    y = coordList[1]
    z = coordList[2]
    w.point(x,y,z)

I made changes to the code so I can do this:

    w = shapefile.Writer(shapefile.POINTZ)
    coordList = [-80, 40, 300]
    w.point(coordList)

Here are the edits to the pyshp code for allowing lists of coordinates:

    def getValue(self, argList, argNum):
        try:
            return argList[argNum]
        except IndexError:
            return 0

    def point(self, x, y=None, z=0, m=0):
        """Creates a point shape."""
        if type(x) is list:
            truex = self.getValue(x, 0)
            y = self.getValue(x, 1)
            z = self.getValue(x, 2)
            m = self.getValue(x, 3)
            x = truex
        elif type(x) is not list and y == None:
            raise ShapefileException('A y-value was not supplied for the point')
        pointShape = _Shape(self.shapeType)
        pointShape.points.append([x, y, z, m])
        self._shapes.append(pointShape)



My second suggestion is to allow a list or dict to be used for writing records:

Currently you must do this:

    w = shapefile.Writer(shapefile.POINTZ)
    fieldNames = ['longitude','latitude','elevation']
    makeFields = [ w.field(field) for field in fieldNames ]
    w.point(-80,40,300)
    fieldValues = ['-80','40','300']
    longitude = fieldValues[0]
    latitude = fieldValues[1]
    elevation = fieldValues[1]
    w.record(longitude,latitude,elevation)

I made changes to the code so I use a list instead of multiple arguments:

    w = shapefile.Writer(shapefile.POINTZ)
    fieldNames = ['longitude','latitude','elevation']
    makeFields = [ w.field(field) for field in fieldNames ]
    w.point(-80,40,300)
    fieldValues = ['-80','40','300']
    w.record(fieldValues)

or use a dict:

    w = shapefile.Writer(shapefile.POINTZ)
    fieldNames = ['longitude','latitude','elevation']
    makeFields = [ w.field(field) for field in fieldNames ]
    w.point(-80,40,300)
    fieldValues = {'longitude': '-80', 'latitude': '45', 'elevation': '300'}
    w.record(fieldValues)


Here are the changes I made to allow for lists or dicts but still allow 
individual args:

    def record(self, *recordList, **recordDict):
        """Creates a dbf attribute record. You can submit either a sequence of
        field values or keyword arguments of field names and values. Before
        adding records you must add fields for the record values using the
        fields() method. If the record values exceed the number of fields the
        extra ones won't be added. In the case of using keyword arguments to specify
        field/value pairs only fields matching the already registered fields
        will be added."""
        record = []
        fieldCount = len(self.fields)
        # Compensate for deletion flag
        if self.fields[0][0].startswith("Deletion"): fieldCount -= 1
        if recordList and type(recordList[0]) is list:
            [record.append(recordList[0][i]) for i in range(fieldCount)]
        elif recordList and type(recordList[0]) is not list and type(recordList[0]) is not dict:
            [record.append(recordList[i]) for i in range(fieldCount)]
        elif recordList and type(recordList[0]) is dict or recordDict:
            recordDict = recordList[0] if recordList and type(recordList[0]) is dict else recordDict
            for field in self.fields:
                if field[0] in recordDict:
                    val = recordDict[field[0]]
                    if val:
                        record.append(val)
                    else:
                        record.append("")
        if record:
            self.records.append(record)



I tested these changes to make sure they don't break the functionality of 
passing in individual arguments, but I was using single file to test and could 
have easily overlooked something.

I used these pyshp changes in a GPX waypoints to SHP script I wrote.  It allows 
me to pass lists to the point and record methods while looping through the 
waypoints.

I think these changes would be beneficial for others and they don't add too 
much to the existing code.

Thanks for the great work.


Original issue reported on code.google.com by [email protected] on 16 Feb 2013 at 9:16

can't write zeros to numeric fields

The logic of line 808:

                if field[0] in recordDict:
                    val = recordDict[field[0]]
         -->        if val:
                        record.append(val)
                    else:
                        record.append("")

...doesn't allow the writing of zeros to numeric fields.

There may be a more robust way to do things, but 'if val != "":' works for me.

What steps will reproduce the problem?
1. try to write a zero to a numeric field

What version of the product are you using? On what operating system?
1.1.4 on winXP

Please provide any additional information below.
Thanks!

Original issue reported on code.google.com by [email protected] on 16 Nov 2011 at 11:30

No good way of incrementally appending data to a file

What steps will reproduce the problem?
1. Get a really large shapefile.
2. Load it in memory.
3. Now try to write it to a file.

What is the expected output? What do you see instead?
The output is exactly as expected, but the memory requirements of the program 
are very high. 
There is no good way of incrementally appending data to an output file. The 
save method can be provided file handlers instead of file names, but it does a 
seek(0) in the __*Header methods and then does a seek(100) in the __*Records 
method and over-rides any data already written to the output file. An "append" 
type version of the save method would be very useful for dealing with large 
files.


Original issue reported on code.google.com by [email protected] on 27 Sep 2012 at 3:47

Cannot create POLYGONZ

What steps will reproduce the problem?

outShp = shapefile.Writer(shapefile.POLYGONZ)
outShp.field('id','N','10')
poly = [ [(0,0,0), (1,0,0), (1,0,1), (0,0,1), (0,0,0)] ]
outShp.poly(poly, shapeType=15)
outShp.record(1)
outShp.save(outShpPath)

What is the expected output? What do you see instead?

File "C:\Python32\lib\site-packages\shapefile.py", line 863, in save
    self.saveShp(target)
File "C:\Python32\lib\site-packages\shapefile.py", line 831, in saveShp
    self.__shpRecords()
File "C:\Python32\lib\site-packages\shapefile.py", line 678, in __shpRecords
    f.write(pack("<%sd" % len(s.z), *s.z))
AttributeError: '_Shape' object has no attribute 'z'

What version of the product are you using? On what operating system?
1.1.6-beta

Please provide any additional information below.

no error with 1.1.4


Original issue reported on code.google.com by [email protected] on 23 Jul 2012 at 12:03

Editor Fails Writing POINTZ Shapefile

What steps will reproduce the problem?
1. Create a POINTZ shapefile using a `Writer()`
2. Attempt to append to the shapefile using an `Editor()`, specifying the 
filename in Editor constructor
3. Write fails with the following exception:

{{{
Traceback (most recent call last):
  File "/Users/driggs2/workspace/batutils/batutils/transect/tkgui_pro.py", line 270, in go
    transect.main()
  File "/Users/driggs2/workspace/batutils/batutils/transect/app.py", line 281, in main
    self._finalize()
  File "/Users/driggs2/workspace/batutils/batutils/transect/app_pro.py", line 90, in _finalize
    self.w.save(self.outfile)
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 862, in save
    self.saveShp(target)
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 829, in saveShp
    self.__shapefileHeader(self.shp, headerType='shp')
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 596, in __shapefileHeader
    raise ShapefileException("Failed to write shapefile elevation and measure values. Floats required.")
ShapefileException: Failed to write shapefile elevation and measure values. 
Floats required.
}}}

What is the expected output? What do you see instead?


What version of the product are you using? On what operating system?
  pyshp 1.1.4
  Python 2.6.6 (Apple)
  Mac OS X 10.6.8

Please provide any additional information below.

Writing the shapefile as POINT rather than POINTZ works correctly.

The above exception itself is due to the Editor's Writer trying to write an "m" 
value of None. For some reason, `Editor.point()` has a different method 
signature than `Writer.point()`, and the former assigns a default value of None 
for "m" and "z", while the latter assigns default value of 0. Changing the 
method signature of `Editor.point()` to use default values m=0, z=0 appears to 
solve the above exception, but still fails later with:

{{{
Traceback (most recent call last):
  File "/Users/driggs2/workspace/batutils/batutils/transect/tkgui_pro.py", line 270, in go
    transect.main()
  File "/Users/driggs2/workspace/batutils/batutils/transect/app.py", line 281, in main
    self._finalize()
  File "/Users/driggs2/workspace/batutils/batutils/transect/app_pro.py", line 93, in _finalize
    self.w.save(self.outfile)
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 862, in save
    self.saveShp(target)
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 830, in saveShp
    self.__shpRecords()
  File "/Users/driggs2/workspace/batutils/batutils/transect/shapefile.py", line 699, in __shpRecords
    f.write(pack("<1d", s.points[0][2]))
IndexError: array index out of range
}}}

Again, I solved my own immediate problem by using POINT type rather than 
POINTZ, but it looks like pyshp may have some more bugs related to editing 
POINTZ types.

Original issue reported on code.google.com by [email protected] on 25 Feb 2012 at 8:54

Pyshp fails without error on invalid DBF files

What steps will reproduce the problem?
1. Load example shapefile from 
http://gis.stackexchange.com/questions/50264/how-to-get-text-attributes-from-sha
pefile-with-python-pyshp/
2. r = shapefile.Reader(
3. len(r.records())

What is the expected output? What do you see instead?

len(r.records()) is 0, I expected 59117.

Please provide any additional information below.

As I noted in my answer on 
http://gis.stackexchange.com/questions/50264/how-to-get-text-attributes-from-sha
pefile-with-python-pyshp/ the DBF file is invalid. It uses an illegal value for 
the DBF deleted flag. Pyshp should either ignore this (I've never seen a DBF 
file in Shapefile context with actual deleted fields) or raise an exception 
upon illegal value.

Perhaps raise an exception by default and provide a lenient mode that ignores 
the flag?

Original issue reported on code.google.com by [email protected] on 7 Feb 2013 at 3:51

Polygon examples should close OR shapefile library should automatically close polygon for you

What steps will reproduce the problem?
1. Create a polygon that is not closed.  
'e.poly(parts=[[[5.1,5],[9.9,5],[9.9,1],[7.5,3],[5.1,1]]])'

What is the expected output? What do you see instead?
A complete shapefile.  IMO, the best solution would be for the polygon creation 
to test if last point is coincident with the first, and if it is do nothing, if 
not append the first point to close the polygon.  Another solution would be to 
make sure the examples all close.

What version of the product are you using? On what operating system?
1.1.4 from PyPi.  Linux.

Please provide any additional information below.
The overall appearance in ArcGIS looks correct.  There is a line closing the 
polygon.  The problem though is that the selection of polygons is strange.

By the way - Thanks for a great bit of software!

Original issue reported on code.google.com by [email protected] on 22 Feb 2012 at 4:10

optimization for appending features

What steps will reproduce the problem?
1.keep appending features to the shape file

What is the expected output? What do you see instead?
The writing time should not increase that much. 

What version of the product are you using? On what operating system?
1.14. Windows

Please provide any additional information below.

A simple testing example is as following:

----------------------------------------------------------------
def append():
    start = datetime.now()
    w = shapefile.Editor(shapefile=r'E:\data\shape.shp')
    w.point(90.3, 30)
    w.record('First', 'Point', datetime.now(), '123213212.22')
    w.save(r'E:/data/shape')
    print(str(i) + ': ' + str((datetime.now() - start).total_seconds()))

for i in range(10000):
    append()
----------------------------------------------------------------

The writing speed keep declining as more features are written, is there any way 
to improve this? I noted in the code, when opening a Editor:

-----------------------------------------
 elif is_string(shapefile):
            base = os.path.splitext(shapefile)[0]
            if os.path.isfile("%s.shp" % base):
                r = Reader(base)
                Writer.__init__(self, r.shapeType)
                self._shapes = r.shapes()
                self.records = r.records()
                self.fields = r.fields
---------------------------------------------

I assume that the writer basically reads all the data into the cache, and then 
rewrite them together with the appended data when "save" is called. Am I right? 
This makes it a real problem after several thousand of records are stored. 

I expect a way that can use the "append" mode to write new features. Is this 
possible?

Original issue reported on code.google.com by [email protected] on 6 Jun 2013 at 8:08

Clearing memory

What steps will reproduce the problem?
1. Creating a temporary shapefile using some function in arcpy
2. Use shapefile to read the shapefile
3. Try to delete/overwrite the shapefile with some arcpy function

What is the expected output? What do you see instead?
Need to some clear the shapefile from memory having read in shapes and records 

e.g. 
sfr = shapefile.Reader(some_file_name)
Recs = sfr.records()
Shapes = sfr.shapes()
#insert sfr.clear() or sfr.close() or something similar here

What version of the product are you using? On what operating system?
1.1.6-beta, Windows 7

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 7 Dec 2012 at 6:12

Duplicate field names cause mismatch between list of field names and fields in records

What steps will reproduce the problem?
1. Load the attached shapefile using the Reader class.
2. Count the number of fields in the Reader and then in any record.
3.

What is the expected output? What do you see instead?
This file contains two fields called "ASIA2000_2".  One of these fields gets 
dropped by the reader.  The ArcGIS "solution" to this problem is to change the 
name of the duplicate field.  Why it lets users create data with duplicate 
fields is beyond me.

What version of the product are you using? On what operating system?
1.1.4, on Mac OS 10.7.5

Please provide any additional information below.
The version of shapefile I am using is embedded in the Basemap library 
(http://matplotlib.org/basemap/).  It might be helpful if the author of that 
library is notified when this package is updated.

I hacked my own solution - basically doing the same thing that ArcGIS is doing. 
 I'd include that but I'm sure you can come up with something a little cleaner.


Original issue reported on code.google.com by [email protected] on 23 Jan 2013 at 11:11

Attachments:

column name with exactly maximum number of characters crashes

What steps will reproduce the problem?
1. Load shapefile with column name that has maximum allowed number of 
characters (then there is no \x00 behind the column name).
2. .index("\x00") raises ValueError

What is the expected output? What do you see instead?


Expected behaviour: read shapefile with such a column name


What version of the product are you using? On what operating system?


Latest download provided.


Please provide any additional information below.

These lines (around 250) should fix the problem (not thoroughly tested, though):

                        if "\x00" in fieldDesc[name]:
                                indx = fieldDesc[name].index("\x00")
                        else:
                                indx = len(fieldDesc[name]) - 1
                        fieldDesc[name] = fieldDesc[name][:indx]

replacing the line

fieldDesc[name] = fieldDesc[name][:fieldDesc[name].index("\x00")]

Original issue reported on code.google.com by [email protected] on 9 Aug 2011 at 6:42

DBF write fails to truncate

Python's rjust and ljust don't seem to truncate, so, around line 735 (and 739) 
in shapefile.py, instead of:
    value = str(value).rjust(size)
I believe you need:
    value = str(value).rjust(size)[:size]

What steps will reproduce the problem?
1. attempt to write floats with higher precision than that alloted in the dbf 
field. 
2.
3.

What is the expected output? A DBF. What do you see instead? An assertion error.

What version of the product are you using? On what operating system?
1.1.4 on Windows XP.

Please provide any additional information below.

Thanks!

Original issue reported on code.google.com by [email protected] on 16 Nov 2011 at 11:08

negative indexes are not well handled in "__restrictIndex" method

What steps will reproduce the problem?

1. open (Reader) a shapefile (with N elements)
2. call the "shape" method with the value -N (which is valid !)

The expected value is the first (index 0) shape object but an exception is 
raised in __restricted index after the test at line 194 (version 1.1.4).

The code below fix the bug

def __restrictIndex(self, i):
    """Provides list-like handling of a record index with a clearer
    error message if the index is out of bounds."""
    if self.numRecords:
        if i < -self.numRecords or self.numRecords <= i:
            raise IndexError("Shape or Record index out of range.")
        if i < 0: i += self.numRecords
    return i

Original issue reported on code.google.com by [email protected] on 9 Apr 2012 at 10:27

Check if shape file exists

What steps will reproduce the problem?
s = shapefile.Reader('<none_existing_file>.shp')
shapes = s.shapes()

What is the expected output? What do you see instead?
Expect an exception stating the file does not exists
Get:
    225         def shapes(self):
    226                 """Returns all shapes in a shapefile."""
--> 227                 self.shp.seek(100)
    228                 shapes = []
    229                 while self.shp.tell() < self.shpLength:

AttributeError: 'NoneType' object has no attribute 'seek'

What version of the product are you using? On what operating system?
1.06
Python 2.6
Ubuntu Linux 10.04 64 bit

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 21 Sep 2011 at 8:09

Problem when using Editor to open files with a dot in their name

What steps will reproduce the problem?
1.Open a file named "a.b.shp" with the editor

What is the expected output? What do you see instead?
The file a.b.shp should be opened. But the editor class
uses:

base = os.path.splitext(shapefile)[0]
            if os.path.isfile("%s.shp" % base):
                r = Reader(base) """here"""
                Writer.__init__(self, r.shapeType)
                self._shapes = r.shapes()
                self.fields = r.fields
                self.records = r.records()

So "a.b" is sent to the reader constructor instead of "a.b.shp".

The reader constructor then uses the splitext function again and sees
it as a file named "a" with a ".b" extension, instead of a file
named "a.b" with a ".shp" extension.

A solution is to replace:
                r = Reader(base)
by
                r = Reader(shapefile)

(line 877)

Original issue reported on code.google.com by [email protected] on 25 Apr 2012 at 8:13

Slot Machine Coding Help

What steps will reproduce the problem?
1.Run the program enter 100 for total, then 10 for bet
2.Keep hitting enter until error appears
3.

What is the expected output? What do you see instead?
expected is in the slots attachment


What version of the product are you using? On what operating system?
python 3.2, windows 7


Please provide any additional information below.
This is a project due tomorrow, need help asap.


Original issue reported on code.google.com by [email protected] on 24 Feb 2012 at 12:49

Editor Constructor Doesn't Call Super

The constructor for `Editor()` optionally takes a `shapefile` param. As 
currently written,

* If no `shapefile` specified, we call the `Writer()` super constructor as if 
we're creating a new shapefile
* If `shapefile` is specified by name, and it exists, we call the `Writer()` 
super constructor, and populate it with the contents of the shapefile read by a 
`Reader() ` instance
* If `shapefile` is specified by name but it does not exist, the `Writer()` 
super constructor is never called and the `_shapes`, `fields`, and `records` 
members never get initialized, causing things to blow up on the first call to 
`point()`.

Trivial patch attached to call the `Writer()` constructor in this case.

Original issue reported on code.google.com by [email protected] on 26 Feb 2012 at 2:43

Attachments:

Date fields with empty days fails

What steps will reproduce the problem?
1. Open a shape file with a date field that has an empty string for the day - 
example: geocommunicator.gov township files


I wrote a simple patch to split on a regex and fail silently if the 
day/month/year cannot be coerced to int.

Original issue reported on code.google.com by [email protected] on 18 Mar 2011 at 2:41

Attachments:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.