赞
踩
最近工作上遇到爬取的高德路况信息数据需要在地图上展示出来,由于json数据不具备直接可视化的能力,又联想到前两个月学习了一点点arcpy的知识,就花了一些时间去写了个代码,毕竟手动处理要了老命了。
json文件显示如下:
通过api和json组织结构解读,我们只需要道路的polyline坐标点空间信息,道路名称,道路方向、道路限速和道路拥堵程度等属性信息。
所以我们需要用python代码去获取roads数组内的信息
读取json文件需要线导入json模块,根据需要,我们还导入了arcpy等模块
- import json
- import os
- import pandas as pd
- import math
- import csv
- import arcpy
- from arcpy import da
- from arcpy import env
- ###打开json文件###
- with open(jsonfile) as f:
- fileread=f.readline()
- jsondata=json.loads(fileread)
- print(jsondata)
- trafficinfo = jsondata['trafficinfo']
- for row in trafficinfo['roads']:
- name = row['name']
- status = row['status']
- direction = row['direction']
- angle = row['angle']
- if "speed" in trafficinfo:
- speed = row['speed']
- lcodes = row['lcodes']
- print ("--------------这里是坐标点的信息------------------------")
- points_data = row['polyline']
- points_data = points_data.split(";")
- #将polyline的坐标点循环遍历取出来
- for i in range(1,len(points_data)):
- print (i)
- print (points_data[i])
- oldLon = float(points_data[i].split(",")[0])
- oldLat = float(points_data[i].split(",")[1])
- lon = gcj02_to_wgs84(oldLon,oldLat)[0]
- lat = gcj02_to_wgs84(oldLon,oldLat)[1]
- pnt = arcpy.Point(lon,lat)
- array.add(pnt)
- polyline = arcpy.Polyline(array,cgcs2000project)
- array.removeAll()
- newFields = [polyline,name,status,direction,angle,speed,lcodes,polyline.length]
- cur.insertRow(newFields)
- del cur
- f.close

- ###创建shp文件###
- print ("创建shp")
- #创建一个空白的线要素
- arcpy.CreateFeatureclass_management(os.path.dirname(shp_output),os.path.basename(shp_output),'POLYLINE',"","","",cgcs2000project)
- #给shp添加字段属性
- arcpy.AddField_management(shp_output,'name','Text')
- arcpy.AddField_management(shp_output,'status','Text')
- arcpy.AddField_management(shp_output,"direction",'Text')
- arcpy.AddField_management(shp_output,'angle','Text')
- arcpy.AddField_management(shp_output,"speed",'Text')
- arcpy.AddField_management(shp_output,"lcodes",'Text')
- arcpy.AddField_management(shp_output,"Length",'Double')
- fields =['SHAPE@',"name", "status", "direction", "angle", "speed", "lcodes",'Length']
- #插入行
- cur = da.InsertCursor(shp_output,fields)
其中outputpath 是输出文件夹路径,
jsonfile是json文件路径
shp_output是输出的文件名称
cgcs2000project是坐标参照
由于高德数据是火星坐标系,我们需要对坐标点进行坐标系转换,将火星坐标系转为CGCS2000坐标系,转换代码在这:https://download.csdn.net/download/THEDEAMON/88807818?spm=1001.2014.3001.5503
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。