2

I have a polygonz shapefile. I have some records which represents building footprints. I want to copy records according to their attributes 'number of storeys value'. For example, a record has storey value as 4. I want to copy footprint 3 times (storey-1) and translate these polygonzs along z axis according to floor heights (which is a constant value as 3m in any case). I did what I want for a record manually using pyshp. However, I have 1282 building footprints as polygon records and I want a more automatic way. Could you suggest any solution? Here is what I achieved as pictureenter image description here

Here is my code

import shapefile

w = shapefile.Writer(shapeType=shapefile.POLYGONZ)
w.field('Name')

w.record('Polygon1')
w.poly([[[570290.3825960495, 4535093.086859931, 0], [570306.3562040495, 4535114.818163932, 0], [570337.0239320495, 4535092.45711193, 0], [570321.0603200498, 4535070.52588793, 0], [570290.3825960495, 4535093.086859931, 0]]], shapeType=15)

w.record('Polygon2')
w.poly([[[570290.3825960495, 4535093.086859931, 3], [570306.3562040495, 4535114.818163932, 3], [570337.0239320495, 4535092.45711193, 3], [570321.0603200498, 4535070.52588793, 3], [570290.3825960495, 4535093.086859931, 3]]], shapeType=15)

w.record('Polygon3')
w.poly([[[570290.3825960495, 4535093.086859931, 6], [570306.3562040495, 4535114.818163932, 6], [570337.0239320495, 4535092.45711193, 6], [570321.0603200498, 4535070.52588793, 6], [570290.3825960495, 4535093.086859931, 6]]], shapeType=15)

w.record('Polygon4')
w.poly([[[570290.3825960495, 4535093.086859931, 9], [570306.3562040495, 4535114.818163932, 9], [570337.0239320495, 4535092.45711193, 9], [570321.0603200498, 4535070.52588793, 9], [570290.3825960495, 4535093.086859931, 9]]], shapeType=15)

w.save("C:/Users/ziyau/Desktop/cesium uygulama/fpz")
1

The following could be a good starting point:

import shapefile

r = shapefile.Reader('footprints.shp')
w = shapefile.Writer(shapeType=shapefile.POLYGONZ)
w.autoBalance = 1
w.field('NAME','C','10')

for i, shapeRec in enumerate(r.shapeRecords()):
    shape = shapeRec.shape
    rec = shapeRec.record

    fields = r.fields[1:]
    field_names = [field[0] for field in fields]
    field_name = 'storeys'
    field_index = field_names.index(field_name)

    storeys = rec[field_index]
    for j in range(storeys):
        new_shape = []
        for k in range(len(shape.points)):
            coords = shape.points[k]
            x, y = float(coords[0]), float(coords[1])
            zeta = shape.z[k]
            z_new = float(zeta) + j * 3 # 3 = floor heights
            new_coords = [x, y, z_new]
            new_shape.append(new_coords)
        w.poly(parts=[new_shape], shapeType=shapefile.POLYGONZ)
        w.record('Polygon{}'.format(j+1))

w.save("storeys.shp")
  • 1
    yes that was exactly what I want thank you @afalciano – user51044 Apr 4 '17 at 12:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.