Pages

Friday, November 4, 2011

Deleting Shapefile Features

Sometimes, usually as a server-based operation, you need to delete all of the features in a shapefile. All you want left is the shapefile type, the dbf schema, and maybe the overall bounding box. This shapefile stub can then be updated by other processes. Pyshp currently doesn't have an explicit "delete" method. But because pyshp converts everything to native Python data types (strings, lists, dicts, numbers) you can usually manipulate things fairly easily. The solution is very similar to merging shapefiles but instead you are writing back to the same file instead of a new copy. There's only one hitch in this operation resulting from a minor difference in the pyshp Reader and Writer objects. In the reader the "bbox" property returns a static array of [xmin, ymin, xmax, ymax]. The Writer also has a "bbox" property but it is a method that is called when you save the shapefile. The Writer calculates the bounding box on the fly by reading all of the shapes just before saving. But in this case there are no shapes so the method would throw an error. So what we do is just override that method with a lambda function to return whatever bbox we want whether it be the original bbox or a made up one.
import shapefile 
# Read the shapefile we want to clear out
r = shapefile.Reader("myshape") 
# Create a Writer of the same type to save out as a blank
w = shapefile.Writer(r.shapeType) 
# This line will give us the same dbf schema 
w.fields = r.fields 
# Use the original bounding box in the header 
w.bbox = lambda: r.bbox 
# Save the featureless, attribute-less shapefile
w.save("myshape") 
Instead of using the original bounding box we could just populate it with 0's and let the update process repopulate it:
w.bbox = lambda: [0.0, 0.0, 0.0, 0.0]
Note coordinates in a shapefile must always be floating-point numbers. Sometimes you may not want to delete all of the features. You may want to select certain features by attribute or using a spatial operation.

3 comments:

  1. check this out, i found the interesting information, so dont miss it.
    http://atmdomino.com

    ReplyDelete
  2. Asam jawa maupun kunyit asam dan buah nanas muda maupun pepaya muda di percaya sebagian orang untuk cara menggugurkan kandungan dengan cara alami . Namun , apakah memang benar terbukti bahan tradisional tersebut dapat untuk cara menggugurkan kandungan ? Mungkin kalau kandunganya masih muda dan lemah bisa saja berefek pada ibu hamil , tapi belum tentu menuntaskan . Bagaimana jika untuk menggugurkan kandungan 4 bulan hal ini tentu sangat sulit sekali , karena usia 4 bulan kondisi kandungan mulai kuat . Apa saja ciri ciri pubertas pada perempuan yang terjadi pada remaja wanita . Perempuan yang sudah memasuki usia 45 tahun biasanya mengalami gejala menopause . Hal tersebut biasanya terjadi kurangnya keluar cairan saat bersegama . Pasca melahirkan wanita biasanya mencari solusi untuk cara menjaga daerah kewanitaan agar tetap rapat dan wangi .

    ReplyDelete