本想将代码文件缀在上一篇的后面,但因为没这个功能,所以新开一篇,放置代码,其中有些是废的,有些是摸索中写的,没有写注释,不知道以后自己能不能看懂。。。。。。哈哈哈哈哈哈哈
import json
import numpy
def openShpJson(fileName):
with open(fileName,'r',encoding='utf-8') as f:
data = json.load(f)
p = data['features'][0]['geometry']['rings'][0]
pstr = json.dumps(p, indent=4)
#pstr = json.dumps(data, indent=4)
#print(pstr)
return p
def openJson(fileName):
with open(fileName,'r',encoding='utf-8') as f:
data = json.load(f)
p = [[float(x) for x in inner] for inner in data]
pstr = json.dumps(p, indent=4)
#pstr = json.dumps(data, indent=4)
print(pstr)
return p
def czml(fileName):
with open(fileName,'r',encoding='utf-8') as f:
data = json.load(f)
p = data[1]['polygon']['positions']['references']
pstr = json.dumps(p, indent=4)
p2 = data[2]
pstr2 = json.dumps(p2, indent=4)
print(pstr)
print(pstr2)
def creat_v(i,time,p):
v = {
"id": "",
"position": {
"interpolationAlgorithm": "LINEAR",
"interpolationDegree": 1,
"interval": "2012-08-04T16:00:00Z/2012-08-04T17:00:00Z",
"epoch": "2012-08-04T16:00:00Z",
"cartographicDegrees": []
}
}
v['id'] = "v%s" % i
cd = v['position']['cartographicDegrees']
cd.extend([time,p[0],p[1],0])
return v
def add_v(v,time,p):
cd = v['position']['cartographicDegrees']
cd.extend([time,p[0],p[1],0])
return v
def ct(pos,pos2):
pos = numpy.array(pos)
pos2 = numpy.array(pos2)
diffMat = pos - pos2
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=0)
distances = sqDistances**0.5
return int(distances*333)
def dist(pos,pos2):
p = numpy.pi/180
a = numpy.sin(pos[1]*p)*numpy.sin(pos2[1]*p)
b = numpy.cos(pos[1]*p)*numpy.cos(pos2[1]*p)*numpy.cos((pos2[0]-pos[0])*p)
d = 6371.004*numpy.arccos(a+b)
return d
def czml_w(fileName,i,pos,pos2,t):
with open(fileName,'r',encoding='utf-8') as f:
data = json.load(f)
p = data[1]['polygon']['positions']['references']
vp = ["v%s#position" % i]
p.extend(vp)
v1 = creat_v(i,0,pos)
v = add_v(v1,t,pos2)
#print(v)
data.append(v)
with open(fileName, 'w',encoding='utf-8') as f:
json.dump(data, f)
#print("ok")
def abstract_f(f1,f2):
with open(f1,'r',encoding='utf-8') as f:
data = json.load(f)
p = data['features'][0]['geometry']['rings'][0]
data['features'][0]['geometry']['rings'][0] = p[::13]
with open(f2, 'w',encoding='utf-8') as ft:
json.dump(data, ft)
print("ok")
def classify0(inX,dataSet,labels,k):
dataSetSize = dataSet.shape[0]
diffMat = tile(inX,(dataSetSize,1)) - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
sortedDistIndicies = distances.argsort()
classCount={}
for i in range(k):
voteIlabel = labels[sortedDistIndicies[i]]
classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
sortedClassCount = sorted(classCount.iteritems(),key = operator.itemgetter(1),reverse = True)
return sortedClassCount[0][0]
def order2p(p1,p2):
p1 = numpy.array(p1)
p2 = numpy.array(p2)
p = []
for x in p1:
dataSetSize = p2.shape[0]
diffMat = numpy.tile(x,(dataSetSize,1)) - p2
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
sortedDistIndicies = distances.argsort()
#p = numpy.vstack((p,p2[sortedDistIndicies[0]]))
p.append(p2[sortedDistIndicies[0]].tolist())
p2 = numpy.delete(p2,sortedDistIndicies[0],axis = 0)
return p
def order2p2(p1,p2):
p = []
for x in p1:
distance = []
for y in p2:
distance.append(dist(x,y))
disarr = numpy.array(distance)
sortdis = disarr.argsort()
p.append(p2[sortdis[0]].tolist())
p2 = numpy.delete(p2,sortdis[0],axis = 0)
return p
def myrange():
mian = openJson('mp.json')
da = openJson('dp.json')
for i in range(len(mian)):
czml_w('czml.json',i,mian[i],da[i],900)
print("ok")
#openJson('mp.json')
myrange()
#p1 = [[0,1],[0,2],[0,3],[0,4],[0,5]]
#p2 = [[3,0],[5,0],[2,0],[1,0],[4,0]]
#print(order2p(p1,p2))
#abstract_f('da.json','da13.json')
#pnum = openShpJson('mian.json')
#pnum2 = openShpJson('da13.json')
#print(len(pnum))
#print(len(pnum2))
#czml('czml.json')
#p1 = [100,90]
#p2 = [110,80]
#myV = creat_v(11,0,p1)
#print(myV)
#print(add_v(myV,600,p2))
#czml_w('czml.json',11)
#p1 = [101.006713,31.3392944]
#p2 = [102.446952,33.5661315]
#print(dist(p1,p2))