1、什么是GeoJSON
GeoJSON是一种对各种地理数据结构进行编码的格式。GeoJSON对象可以表示几何、特征或者特征集合。GeoJSON支持下面几何类型:点、线、面、多点、多线、多面和几何集合。GeoJSON里的特征包含一个几何对象和其他属性,特征集合表示一系列特征。一个完整的GeoJSON数据结构总是一个(JSON术语里的)对象。在GeoJSON里,对象由名/值对--也称作成员的集合组成。对每个成员来说,名字总是字符串。
下面是一个点要素较为标准的geoJson格式:
{
"type": "FeatureCollection",
"features": [
{"type":"Feature",
"properties":{},
"geometry":{
"type":"Point",
"coordinates":[105.380859375,31.57853542647338]
}
}
]
}
为了转为js格式文件或者存储在xml以便于前端使用,需要对txt文件进行处理转化为geoJSON格式。
2、处理的测试数据
以下为txt格式的数据摘要,主要由四列组成,第一列为编号id,第二列为时间戳,第三列为经度,第四列为纬度
7bec7c291270f973ebd30f93db5ccafa,1525140513,104.05878,30.50994
7bec7c291270f973ebd30f93db5ccafa,1525140516,104.05873,30.50987
7bec7c291270f973ebd30f93db5ccafa,1525140519,104.05870,30.50983
7bec7c291270f973ebd30f93db5ccafa,1525140522,104.05869,30.50981
7bec7c291270f973ebd30f93db5ccafa,1525140525,104.05867,30.50979
7bec7c291270f973ebd30f93db5ccafa,1525140528,104.05862,30.50972
7bec7c291270f973ebd30f93db5ccafa,1525140531,104.05855,30.50963
7bec7c291270f973ebd30f93db5ccafa,1525140534,104.05847,30.50953
3、Python转化代码
import numpy as np
from collections import defaultdict
import json
import copy
#总字典
objects=[]
#**********************读取文件*********************************
fileName="testData.txt"
location = []
list1=[]
#读取txt文件中每一行的值
for line in open(fileName, "r"):
#每个输入数据以逗号隔开
items = line.strip("\n").split(",")
# label.append(int(items.pop()))
item=items[1:2]
list1.append(item)
tmps=[]
for j in range(2,4):
tmp=float(items[j])
tmps.append(tmp)
location.append(tmps)
#location = np.array(location)
#location=location.astype(float)
#np.savetxt("locationList.txt",location,fmt="%s")
list1=np.array(list1)
#ndarray类型
#np.savetxt("timeNump.txt",list,fmt="%d")
list1=list1.reshape(-1)
list2=[]
for i in list1:
temp=i+"000"
i=copy.copy(temp)
list2.append(i)
list2=np.array(list2)
list2=list2.astype(np.uint64)
list2=list2.tolist()
#******************************************读取文件结束********************************************
#location存储的是位置坐标
#list2存储的是时间戳
#geometry属性的字典
geo=defaultdict(list)
geo["type"]="MultiPoint"
for i in location:
geo["coordinates"].append(i)
#properties属性的字典
pro=defaultdict(list)
for i in list2:
pro["time"].append(i)
pro["speed"]
pro["altitude"]
pro["heading"]
pro["horizontal_accuracy"]
pro["vertical_accuracy"]
pro["raw"]
object={
'type': "Feature",
'geometry':geo,
'properties':pro,
"bbox" : []
}
objects.append(object)
object1={
'type': "Feature1",
'geometry':geo,
'properties':pro,
"bbox" : []
}
objects.append(object1)
json_str = json.dumps(objects, indent=2)#注意这个indent参数
with open('object_data.js', 'w') as json_file:
json_file.write(json_str)
要特别注意的是时间戳我将其处理为后面加上000即乘以1000.
4、处理结果
下面为一个id对应的一组数据,每个id合起来组成最终的json文件。
{
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.0804,
30.49633
],
[
104.08042,
30.49634
],
[
104.08053,
30.49635
],
[
104.0807,
30.49637
],
[
104.08078,
30.49638
],
[
104.08083,
30.49639
],
[
104.08093,
30.4964
],
[
104.08109,
30.49642
],
[
104.08129,
30.49644
],
[
104.08155,
30.49645
],
[
104.08179,
30.49644
],
[
104.08212,
30.49643
],
[
104.0825,
30.4964
],
[
104.08285,
30.49637
],
[
104.08317,
30.49635
],
[
104.08345,
30.49633
],
[
104.08362,
30.49632
],
[
104.0837,
30.49631
],
[
104.08377,
30.4963
],
[
104.08394,
30.4963
],
[
104.08415,
30.4963
],
[
104.08439,
30.4963
],
[
104.08469,
30.4963
]
]
},
"properties": {
"time": [
1525161601000,
1525161604000,
1525161607000,
1525161610000,
1525161613000,
1525161616000,
1525161619000,
1525161622000,
1525161625000,
1525161628000,
1525161631000,
1525161634000,
1525161637000,
1525161640000,
1525161643000,
1525161646000,
1525161649000,
1525161652000,
1525161655000,
1525161658000,
1525161661000,
1525161664000,
1525161667000,
1525161670000,
1525161673000,
1525161676000,
1525161679000,
1525161682000,
1525161685000,
1525161688000,
1525161691000,
1525161694000,
1525161697000,
1525161700000,
1525161703000,
1525161706000,
1525161709000,
1525161712000,
1525161715000,
1525161718000,
1525161721000,
1525161724000,
1525161727000,
1525161730000,
1525161733000
],
"speed": [],
"altitude": [],
"heading": [],
"horizontal_accuracy": [],
"vertical_accuracy": [],
"raw": []
},
"bbox": []
}