shapefile_reader.py - improve by adding test_polygon

This commit is contained in:
artemp 2015-10-13 11:24:02 +01:00
parent b75f075885
commit 4093f10f61

View file

@ -25,6 +25,8 @@ def test_record(_type, record) :
print "NULL shape" print "NULL shape"
elif _type == 11: #PointZ elif _type == 11: #PointZ
test_pointz(record) test_pointz(record)
elif _type == 5:
test_polygon(record)
def test_pointz(record): def test_pointz(record):
if len(record) != 36 : if len(record) != 36 :
@ -35,6 +37,17 @@ def test_pointz(record):
print>>sys.stderr,"BAD SHAPE FILE: expected PointZ or NullShape got",_type print>>sys.stderr,"BAD SHAPE FILE: expected PointZ or NullShape got",_type
sys.exit(1) sys.exit(1)
def test_polygon(record):
_type, x0, y0, x1, y0, num_parts, num_points = struct.unpack("<iddddii", record[0:44])
if _type != 5:
print>>sys.stderr, "BAD SHAPE FILE: expected Polygon or NullShape got", _type
sys.exit(1)
length = len(record)
rec_length = 44 + num_parts * 4 + num_points * 16
if rec_length <> length:
print>>sys.stderr, "BAD SHAPE FILE: expected", rec_length, "got", length
sys.exit(1)
if __name__ == "__main__" : if __name__ == "__main__" :
if len(sys.argv) !=2: if len(sys.argv) !=2:
@ -52,10 +65,17 @@ if __name__ == "__main__" :
_,_,_,_,_,_,shx_file_length = header[0].unpack_from(shx.read(28)) _,_,_,_,_,_,shx_file_length = header[0].unpack_from(shx.read(28))
_,_,lox,loy,hix,hiy,_,_,_,_ = header[1].unpack_from(shx.read(72)) _,_,lox,loy,hix,hiy,_,_,_,_ = header[1].unpack_from(shx.read(72))
shx_bbox = [lox,loy,hix,hiy]
# SHP header # SHP header
_,_,_,_,_,_,shp_file_length = header[0].unpack_from(shp.read(28)) _,_,_,_,_,_,shp_file_length = header[0].unpack_from(shp.read(28))
version,_type,lox,loy,hix,hiy,_,_,_,_ = header[1].unpack_from(shp.read(72)) version,_type,lox,loy,hix,hiy,_,_,_,_ = header[1].unpack_from(shp.read(72))
shp_bbox = [lox,loy,hix,hiy]
if shx_bbox <> shp_bbox :
print "BAD SHAPE FILE: bounding box mismatch in *.shp and *.shx", shp_bbox, shx_bbox
sys.exit(1)
print "SHX FILE_LENGTH=",shx_file_length,"bytes" print "SHX FILE_LENGTH=",shx_file_length,"bytes"
print "SHP FILE_LENGTH=",shp_file_length,"bytes" print "SHP FILE_LENGTH=",shp_file_length,"bytes"
@ -69,7 +89,6 @@ if __name__ == "__main__" :
offset,shx_content_length = record.unpack_from(shx.read(8)) offset,shx_content_length = record.unpack_from(shx.read(8))
shp.seek(offset*2, os.SEEK_SET) shp.seek(offset*2, os.SEEK_SET)
record_number,content_length = record_header.unpack_from(shp.read(8)) record_number,content_length = record_header.unpack_from(shp.read(8))
if shx_content_length <> content_length: if shx_content_length <> content_length:
print "BAD SHAPE FILE: content_lenght mismatch in SHP and SHX",shx_content_length,content_length print "BAD SHAPE FILE: content_lenght mismatch in SHP and SHX",shx_content_length,content_length
sys.exit(1) sys.exit(1)