I generate a shapefile from postgresql's postgis table (I used django pyshp library). My shapefile looks valid, it was successfully opened with ArcGIS 10.1, 10.2, 10.3. But I have a problem: If I save edits of a point's coordinates or edit an attribute table, a space symbol appears at the start of the attribute cells. I can edit only one cell, and every cell in every object has started with a space. So the last symbols are migrated to the next cell if cell's size is not enough. So values become wrong and I am unable to use the data.

But I can save my original shapefile as new layer and then open it as a new shapefile. And when I do that, I can edit all I need and all cells stay valid.

Why? What can be wrong with my original shapefile? And why would saving it as a new shapefile fix the problem?

Update: When I try to edit something using QGIS, all objects stay valid and the same issue does not occur.

Now I add the python-2.7 code creating shapefile: full code is too long

import zipfile
import shapefile as sh
self.sh = sh.Writer(hash_geom[self.geometry])
# create a dbf fields list: (name, type, length), default type = C
field_type = FIELDTYPE.get(field.get_internal_type())
if field_type == 'N' or field_type == 'F':
    self.sh.field(str(key), field_type, lengthOfData, 8)
elif field_type == 'L':
    self.sh.field(str(key), 'L', 1)
    self.sh.field(str(key), field_type, lengthOfData, 0)
    # for every record I append value to values list:
# then I append full record as new object attributes:
# then I append geometry
if self.geometry in ['POINT', 'MULTIPOINT']:
        p1, p2 = self.coords
        self.sh.point(p1, p2)
 elif self.geometry in ['POLYGON', 'MULTIPOLYGON']:
# then I create a shapefile as zip from stream of records:
from StringIO import StringIO
stream = StringIO()
self.shp = StringIO()
self.shx = StringIO()
self.dbf = StringIO()
z = zipfile.ZipFile(stream, 'w', zipfile.ZIP_DEFLATED)
z.writestr(self.modelname + '.shp', self.shp.getvalue())
z.writestr(self.modelname + '.shx', self.shx.getvalue())
z.writestr(self.modelname + '.dbf', self.dbf.getvalue())
z.writestr(self.modelname + '.prj', 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]]')
  • This seems to be a coding question so can you include a code snippet that illustrates how you created one of your odd shapefiles, please? – PolyGeo Aug 10 '16 at 20:19
  • @PolyGeo , done. – Mae Aug 11 '16 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.