代码:
# -*- 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")