首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用pyshp调整多边形的大小

用pyshp调整多边形的大小
EN

Stack Overflow用户
提问于 2018-03-21 08:26:20
回答 1查看 262关注 0票数 1

Hi,我想编写一个脚本来调整现有shapefile中多边形的大小,并使用PyShp库.保存它。

代码语言:javascript
复制
enter code here
import shapefile

fileName = "" ##file name
r = shapefile.Reader(fileName)
records = r.records()
w = shapefile.Writer(r.shapeType)
w.fields = r.fields[1:]

size = len(list(r.iterShapes()))
for i in range(1, size):
    t = r.shapeRecords()[i]
    info = t.shape.__geo_interface__
    allPoints = info['coordinates']
    w.fields = list(r.fields)
    w.records.extend(r.records())
    w._shapes.extend(r.shapes())

    points = allPoints[0]
    point1 = points[0]
    point2 = points[1]
    point3 = points[2]
    point4 = points[3]

    avgX = (point1[0] + point2[0] + point3[0] + point4[0])/4
    avgY = (point1[1] + point2[1] + point3[1] + point4[1])/4

    newX1 = 1.7*(point1[0] - avgX) + avgX
    newX2 = 1.7*(point2[0] - avgX) + avgX
    newX3 = 1.7*(point3[0] - avgX) + avgX
    newX4 = 1.7*(point4[0] - avgX) + avgX

    newY1 = 1.7*(point1[1] - avgY) + avgY
    newY2 = 1.7*(point2[1] - avgY) + avgY
    newY3 = 1.7*(point3[1] - avgY) + avgY
    newY4 = 1.7*(point4[1] - avgY) + avgY

    newPoint1 = [newX1, newY1]
    newPoint2 = [newX2, newY2]
    newPoint3 = [newX3, newY3]
    newPoint4 = [newX4, newY4]

    newPoints = [newPoint1, newPoint2, newPoint3, newPoint4, newPoint1, 
    newPoint1]

 w.save('')

这就是我走了这么远。现在我的问题是如何正确地编写新的shapefile?

EN

回答 1

Stack Overflow用户

发布于 2018-10-02 16:01:44

我注意到这个之前没有回答的问题,所以我用它来提高我的pyshp技能:)。为了将新的点保存到多边形,您错过了w.poly()。

代码语言:javascript
复制
import shapefile

fileName = r"shapefile.shp"
r = shapefile.Reader(fileName)

w = shapefile.Writer(r.shapeType)
w.fields = r.fields[1:]

for shprec in r.iterShapeRecords():
    points = shprec.shape.points

    point1 = points[0]
    point2 = points[1]
    point3 = points[2]
    point4 = points[3]

    avgX = (point1[0] + point2[0] + point3[0] + point4[0])/4
    avgY = (point1[1] + point2[1] + point3[1] + point4[1])/4

    newX1 = 1.7*(point1[0] - avgX) + avgX
    newX2 = 1.7*(point2[0] - avgX) + avgX
    newX3 = 1.7*(point3[0] - avgX) + avgX
    newX4 = 1.7*(point4[0] - avgX) + avgX

    newY1 = 1.7*(point1[1] - avgY) + avgY
    newY2 = 1.7*(point2[1] - avgY) + avgY
    newY3 = 1.7*(point3[1] - avgY) + avgY
    newY4 = 1.7*(point4[1] - avgY) + avgY

    newPoint1 = [newX1, newY1]
    newPoint2 = [newX2, newY2]
    newPoint3 = [newX3, newY3]
    newPoint4 = [newX4, newY4]

    newPoints = [newPoint1, newPoint2, newPoint3, newPoint4, newPoint1, 
    newPoint1]

    w.poly(parts = [newPoints])
    w.record(shprec.record[0])

w.save(r"shapefile_resized.shp")

我以为多边形只有4个顶点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49401347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档