geodatabase数据导入MongoDB

工具arcpy,pymongo(MongoDB的python驱动)

软件版本:arcpy(10.1),MongoDB(2.4 支持点线面)

导入脚本:

# -*- coding: cp936 -*-

import arcpy
import pymongo
from pymongo import MongoClient



def main():
    client = MongoClient('localhost', 27017)
    path="D:/mlxc.gdb/MLXC/"
    arcpy.env.workspace=path
    db = client['NodeDB']
    handlePolyline(db)
    handlePoint(db)

            
#polylineList 对线的转换
def handlePolyline(db):
        fcList = arcpy.ListFeatureClasses(feature_type='Polyline')
        for fc in fcList:
            fieldList = arcpy.ListFields(fc)
            fields = [field.name for field in fieldList]
            fields.pop()
            fields.append("SHAPE@")
            lines=db[fc]
            with arcpy.da.SearchCursor(fc, fields) as cursor:
                print 'Handling '+fc
                for row in cursor:
                    line={}
                    i=0
                    for value in row:
                        if(isinstance(value,arcpy.Polyline)):
                            loc={"type" : "LineString"}
                            coordinates=[]
                            for path in value:
                                for point in path:
                                    coordinates.append([point.X,point.Y])   
                            loc['coordinates']=coordinates
                            line['line']=loc
                        else:
                            line[fields[i]]=value
                        i=i+1
                    lines.insert(line)

                        
                        
#pointList 对点的转换
def handlePoint(db):
        fcList = arcpy.ListFeatureClasses(feature_type='Point')
        for fc in fcList:
            fieldList = arcpy.ListFields(fc)
            fields = [field.name for field in fieldList]
            fields.pop()
            fields.append("SHAPE@")
            points=db[fc]
            with arcpy.da.SearchCursor(fc, fields) as cursor:
                print 'Handling '+fc
                for row in cursor:
                    pointfc={}
                    i=0
                    for value in row:
                        if(isinstance(value,arcpy.PointGeometry)):
                            loc={"type" : "Point"} 
                            loc['coordinates']=[value.firstPoint.X,value.firstPoint.Y]
                            pointfc['point']=loc
                        else:
                            pointfc[fields[i]]=value
                        i=i+1
                    points.insert(pointfc)
                
                
if __name__ == '__main__':
    main()

 

 

 

 

 

 

posted @ 2013-07-19 12:20  rovoqo  阅读(433)  评论(0编辑  收藏  举报