2020年4月9号更新,把数据写出为geojson就好了。
所以说为什么geojson用的这么多,新的数据格式替代shapefile绝对是因为新数据格式优越好用啊!!!
随便吐槽一下TensorFlow难用,以及torch好用的。
等我有空来更博。
目前还是一名TensorFlow和pytorch的小白。
------------------------------以下是历史问题-------------------
关键词:
shapely gdal 写出线要素为空
shapely ZeroDivisionError geopandas
6月5号,今天终于好了。感谢刘良和孙志伟小兄弟!!!
之前把读写都放这个LoadData.py就不行
用graphicTest 里面import LoadData as L_data
然后
L_data.export_shapely_linestring(axis,test_file_path + ‘axis6.shp’)
写出的线要素就是空的。。。。
但是放一个py就好了。。。。我就是个菜鸟。。。。为什么啊。。。。。
暂时还不说我的python3.7(显示为python3.6)的geopandas和shapely之间库依赖关系导致的shapely最小外接矩形函数不能用,报错ZeroDivisionError 的问题
1.原先的python环境有两个,3.7(显示是3.6)和创建的3.5,python3.7安装了shapely(1.6.4.post2),gdal(2.3.3),rtree(0.8.3)等。
python3.5是为了TensorFlow安装的,环境有,shapely(1.6.4.post2),gdal(2.4.1) rtree(0.8.3)
2.gdal 把shapely的linestring写出为shapefile的时候,写出的线要素是空的,属性表是好的,所以又安装了conda install -c conda-forge geopandas 安装所有geopandas依赖库包
3.但是这时,python3.7的shapely的最小外接矩形函数不能用了
显示的是
4.万幸的是,python3.5的shapely的最小外接矩形函数和长轴短轴依然能够计算,所以问题回到了,gdal输出线要素文件有问题。。。geopandas别人都说好,为什么安装了以后shapely函数就不能用
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1668, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1662, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2017.3.4\helpers\pydev\pydevd.py", line 1072, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2017.3.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/mapgeneralization/rastertest/graphicTest.py", line 395, in <module>
shp5_test(test_file_path + 'move27.geojson')
File "D:/mapgeneralization/rastertest/graphicTest.py", line 352, in shp5_test
recti = MultiPoint(polygons[i]).minimum_rotated_rectangle
File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geometry\base.py", line 529, in minimum_rotated_rectangle
_transformed_rects(), key=lambda r: r[0].area)
File "C:\ProgramData\Anaconda3\lib\site-packages\shapely\geometry\base.py", line 515, in _transformed_rects
ux, uy = dx / length, dy / length
ZeroDivisionError: float division by zero
附上最小外接矩形的测试函数(目的是取所有矩形的长轴短轴再存一个线要素文件)
def shp5_test(geojsonfilename):
'''geojson file,还是shapely的最小外接矩形的轮子好用'''
# 'D:/mapgeneralization/data/bj17seg/bj17p1.geojson'
shpdata2 = L_data.load_data_geojson(geojsonfilename)
# 对建筑物中心建立三角网还是建筑物的顶点建立三角网??
polygon_centers = []
polygons = []
poly_num = len(shpdata2['features'])
for i in range(0, poly_num):
cor_i = shpdata2['features'][i]['geometry']['coordinates']
polygons.append(cor_i[0][0])
# cen_x, cen_y = GeoUtil.get_center(cor_i)
# polygon_centers.append([cen_x, cen_y])
# 测试最小外接矩形和长短轴
rects = []
for i in range(0, poly_num):
point_num = len(polygons[i])
# if (polygons[i][0][0] == polygons[i][point_num-1][0])and(polygons[i][0][1] == polygons[i][point_num-1][1]):
# del polygons[i][point_num-1]
recti = MultiPoint(polygons[i]).minimum_rotated_rectangle
rects.append(recti)
#L_data.export_shpely_pos(rects,'D:/mapgeneralization/D3B/D3B/move27/rects7.shp')
num2 = len(rects)
shortaxiss,longaxiss = [],[]
axis = []
for i in range(0,num2):
# get the minimum bounding rectangle and zip coordinates into a list of point-tuples
mbr_points = list(zip(rects[i].exterior.coords.xy))
xx = mbr_points[0]
yy = mbr_points[1]
x0,x1 = xx[0][0],xx[0][1]
y0,y1 = yy[0][0],yy[0][1]
line0 = LineString([(x0,y0),(x1,y1)])
n_xx = len(xx[0])
# for i in range(0,4):
# print(i)
# calculate the length of each side of the minimum bounding rectangle
mbr_lines = [LineString([(xx[0][i],yy[0][i]),(xx[0][i+1], yy[0][i + 1])]) for i in range(0,n_xx - 1)]
mbr_lengths = [LineString([(xx[0][i],yy[0][i]),(xx[0][i+1], yy[0][i + 1])]).length for i in range(0,n_xx - 1)]
# get major/minor axis measurements
minor_axis = min(mbr_lengths)
major_axis = max(mbr_lengths)
short_axis = None
long_axis = None
for i in range(0,len(mbr_lines)):
if mbr_lines[i].length == minor_axis:
short_axis = mbr_lines[i]
if mbr_lines[i].length == major_axis:
long_axis = mbr_lines[i]
shortaxiss.append(short_axis)
longaxiss.append(long_axis)
axis.append(longaxiss)
axis.append(shortaxiss)
L_data.export_linestring3(axis[0],test_file_path + 'axis4.shp')