python操作mongodb数据库,存入GPS坐标点数据

1.安装Mongodb 64位版本,安装mongobooster数据库管理器,有关MongoDB的行管问题,通过百度就可以学习

2.通过python操作mongodb数据库,最主要的就是使用pymongo库了,我们仍然需要去学习官方文档(http://api.mongodb.com/python/current/api/pymongo/

3.我们主要讲的不是pymongo的操作,而是如何读写GPS坐标点数据,我们主要用到的事GeoJSON数据格式,这是一种以json格式为基础用来进行地理数据交换的数据。具体关于GeoJSON的内容,我们可以查阅(https://blog.csdn.net/yaoxiaochuang/article/details/53117379)进行学习。

4.把GPS坐标点数据写入数据库

# -*- coding: utf-8 -*-
from pymongo import MongoClient
client = MongoClient()
db = client.points
posts = db.points
point1={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03738832473755,
          36.79484591011292
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03727567195892,
          36.79410918598529
        ]
      }
    }
  ]
}
point2={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03749561309816,
          36.79484376223946
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03736686706542,
          36.79410703809117
        ]
      }
    }
  ]
}
point3={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03762435913086,
          36.79483946649236
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.0374902486801,
          36.79410059440848
        ]
      }
    }
  ]
}
point4={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03774774074554,
          36.79483517074504
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03760290145873,
          36.79410059440848
        ]
      }
    }
  ]
}
point5={
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03787112236023,
          36.794826579249644
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          118.03772091865538,
          36.794104890197
        ]
      }
    }
  ]
}
point1['id']=0
point2['id']=1
point3['id']=2
point4['id']=3
point5['id']=4
post_id = posts.insert_one(point1).inserted_id
post_id = posts.insert_one(point2).inserted_id
post_id = posts.insert_one(point3).inserted_id  
post_id = posts.insert_one(point4).inserted_id
post_id = posts.insert_one(point5).inserted_id                            

4.应用实例,计算上述5条线段生产的缓冲区的交集和交集的面积

# -*- coding: utf-8 -*-
from pymongo import MongoClient
import geopandas.geoseries
from shapely.geometry import LineString  
client = MongoClient()
db = client.points
posts = db.points
def BUFFER(i):#缓冲区函数
    c=posts.find_one({"id":i})
    list2=[]
    for i in range(0,len(c["features"])):
        list2.append(c["features"][i]['geometry']['coordinates'])
    l1=LineString(list2)
    g1=geopandas.GeoSeries(l1)
    g1.crs={'init' :'epsg:4326'}
    #g1.plot()
    g1=g1.to_crs({'init': 'epsg:4793'})
    l2=g1[0].buffer(7,cap_style=2)
    g2=geopandas.GeoSeries(l2)
    #g2.plot()
    g2.crs={'init' :'epsg:4793'}
    a=g2[0].area
    b=g2[0]
    g2=g2.to_crs({'init': 'epsg:4326'})
    #g1.plot()
    #g2.plot()
    c=g2[0]
    d=g2[0].bounds
    return(a,b,c,d)
'''for i in range(posts.count()):
    print(BUFFER(i)[0])#面积
    print(BUFFER(i)[2])#缓冲区范围,经纬度坐标
    print(BUFFER(i)[3])#缓冲区bbox'''
gg=[]
garea=[]
for i in range(posts.count()-1):
    p=BUFFER(i)[1]&BUFFER(i+1)[1]#求交集
    g3=geopandas.GeoSeries(p)
    g3.crs={'init' :'epsg:4793'}
    #print("面积:%s"%g3[0].area)
    garea.append(g3[0].area)
    g3=g3.to_crs({'init': 'epsg:4326'})
    #g3=g3.to_json()
    gg.append(g3[0])
    #print(gg)
    #print(g3)
    #print(g3[0])
    #print("bbox:",g3[0].bounds)

g1=[]#交集

g4=[]#全部的

for i in range(posts.count()-2):
    p=BUFFER(i)[1]&BUFFER(i+1)[1]#求交集
    g1.append(p)
    
for i in range(posts.count()):
    p=BUFFER(i)[1]
    g4.append(p)
    
g5=geopandas.GeoSeries(g4)
g5.crs={'init' :'epsg:4793'}
g5=g5.to_crs({'init': 'epsg:4326'})

g=geopandas.GeoSeries(gg)
#g.crs={'init' :'epsg:4793'}
#g=g.to_crs({'init': 'epsg:4326'})

g5=g5.to_json()

g=g.to_json()

print(g)

    
      
    

猜你喜欢

转载自blog.csdn.net/qq_912917507/article/details/81095228