线段间以点填充(Python2)

代码:

# -*- coding: utf-8 -*-
from __future__ import division
import math

#            x1                               y1                            x2                               y2
#            point[0][0]                 point[0][1]                point[1][0]                 point[1][1]
cc = [[11.95767795114599, 51.49321401214662], [11.95822799507401, 51.49322057779965]]


# 通过输入的两个点计算一元一次方程,通过输入x,计算y
def designFormulas(point, x):
    a = (point[1][1] - point[0][1]) / (point[1][0] - point[0][0])
    b = (point[1][0] * point[0][1] - point[1][1] * point[0][0]) / (point[1][0] - point[0][0])
    y = a * x + b
    return y


# 计算两个点之间的距离
def calLength(point):
    dis = math.sqrt(pow((point[0][0] - point[1][0]), 2) + pow((point[0][1] - point[1][1]), 2))
    return dis


# 均匀在一条直线上生成指定个数点
# 是否包涵首末位点(True的时候包涵,False时不包涵)
# 包涵首末点时,比所要个数多首末两个点
def calPointsListByCoord(point, count, flag):
    simulatePoints = []
    if flag:
        simulatePoints = [point[0]]
    if point[1][0] == point[0][0] and point[1][1] != point[0][1]:
        yDis = point[1][1] - point[0][1]
        yAve = yDis / (count + 1)
        y = point[0][1] + yAve
        pointCount = 0
        while pointCount < count:
            x = point[0][0]
            simulatePoint = [x, y]
            y += yAve
            simulatePoints.append(simulatePoint)
            pointCount += 1
    elif point[1][1] == point[0][1] and point[1][0] != point[0][0]:
        xDis = point[1][0] - point[0][0]
        xAve = xDis / (count + 1)
        x = point[0][0] + xAve
        pointCount = 0
        while pointCount < count:
            y = point[0][1]
            simulatePoint = [x, y]
            x += xAve
            simulatePoints.append(simulatePoint)
            pointCount += 1
    elif point[1][1] == point[0][1] and point[1][0] == point[0][0]:
        return simulatePoints
    else:
        a = (point[1][1] - point[0][1]) / (point[1][0] - point[0][0])
        b = (point[1][0] * point[0][1] - point[1][1] * point[0][0]) / (point[1][0] - point[0][0])

        slope = (point[1][1] - point[0][1]) / (point[1][0] - point[0][0])
        if slope >= 0.5:
            yDis = point[1][1] - point[0][1]
            yAve = yDis / (count + 1)
            y = point[0][1] + yAve
            pointCount = 0
            while pointCount < count:
                x = (y - b) / a
                simulatePoint = [x, y]
                y += yAve
                simulatePoints.append(simulatePoint)
                pointCount += 1
        else:
            xDis = point[1][0] - point[0][0]
            xAve = xDis / (count + 1)
            x = point[0][0] + xAve
            pointCount = 0
            while pointCount < count:
                y = a * x + b
                simulatePoint = [x, y]
                x += xAve
                simulatePoints.append(simulatePoint)
                pointCount += 1
    # 是否包涵末位点
    if flag:
        simulatePoints.append(point[1])
    return simulatePoints


# 测试calPointsListByCoord()
# cnm = [[10, 20], [20, 40]]
# print calPointsListByCoord(cnm, 3, False)

# 计算多个相连线段,在多个线段上放点(由于首位点相连,中间线段的首位点会重复)
def calAllPointsList(points, count):
    separatedLines = []
    linesLengths = []
    for index, i in enumerate(points):
        if index < len(points) - 1:
            line = [points[index], points[index + 1]]
            lineLength = calLength(line)
            linesLengths.append(lineLength)
            separatedLines.append(line)

    sumLength = sum(linesLengths)
    linesCounts = []
    for i in linesLengths:
        lineCount = int(round(count * (i / sumLength)))
        linesCounts.append(lineCount)

    allLinePoints = []
    for index, i in enumerate(separatedLines):
        thisPointCount = linesCounts[index]
        if thisPointCount == 0:
            continue
        thisPoints = calPointsListByCoord(i, thisPointCount, True)
        allLinePoints.extend(thisPoints)

    allLinePoints.extend(points)
    return allLinePoints


# 多条首位相连的线的集合
def calMultAllPointsList(lines, count):
    linesPoints = []
    for i in lines:
        allPoints = calAllPointsList(i, count)
        linesPoints.extend(allPoints)
    return linesPoints


# 根据生成的点创建geojson文件
def createGeojson(points, pointJsonFile):
    pointsGeoStr = '{"type":"FeatureCollection","features":['
    for i in points:
        pointsGeoStr += '{"geometry":{"coordinates":'
        pointsGeoStr += str(i)
        pointsGeoStr += ',"type":"Point"},"properties":{},"type":"Feature"},'
    pointsGeoStr = pointsGeoStr[:-1]
    pointsGeoStr += ']}'
    linksFile = open(pointJsonFile, 'w')
    linksFile.write(pointsGeoStr)


cnm = [[[10, 30], [50, 30], [20, 10], [30, 45], [40, 10], [10, 30]],
       [[10, 30], [30, 45], [50, 30], [40, 10], [20, 10], [10, 30]]]
createGeojson(calMultAllPointsList(cnm, 100), "D:/pointssss.geojson")

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纯洁的小魔鬼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值