ArcPy (7)简单的建立文件地理数据库、要素集

要求:

1、创建一个File Geodatabase
2、创建一个FeatureClass,geometry类型为Point、采用GCS_North_American_1983坐标系统,该FeatureClass的属性字段包含经度、纬度以及State_Name字段。
3、使用InsertCusor将文本文件中的坐标信息导入到FearureClass
4、使用UpdateCursor,用usa.mxd中的"State"图层中的"State_Name"字段来更新FeatureClass的“State_Name”字段。

在这里插入图片描述
文本文件textToFC.txt中的坐标信息:
在这里插入图片描述


作答:

  (1)创建文件地理数据库
在这里插入图片描述
  (2)创建要素类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
    添加字段
在这里插入图片描述
在这里插入图片描述

  (3)使用InsertCursor 将文本文件中的坐标信息导入到FeatureClass中
在这里插入图片描述
在这里插入图片描述
  (4)①MapXY.shp与usa.mxd中的States图层进行 空间连接 操作,得到Join_MapXY.shp
在这里插入图片描述
在这里插入图片描述
    ②然后用UpdateCursor,用Join_MapXY.shp中“STATE_NAME_1”字段的值 来更新MapXY.shp的“State_Name”字段
在这里插入图片描述
在这里插入图片描述

 下面是把代码做一下整合,写在Pycharm中的
 因为Pycharm的ArcPy环境配置有点问题,所以没法运行做调试(可能代码中有些地方会有些许错误)…
 等以后空了的话,再对这段代码做下调试

# 引用系统模块
import os
import sys
import arcpy


# 创建文件地理数据库
out_FileGDB_path = "D:\EX08\data"
out_FileGDB_name = "EX08.gdb"
arcpy.CreateFileGDB_management(out_FileGDB_path,out_FileGDB_name)


# 创建要素类
# geometry类型为Point、采用GCS_North_American_1983坐标系统
out_FC_path = "D:\EX08\data\EX08.gdb"
out_FC_name = "MapXY"
geometry_type = "Point"
spatial_reference = 4269 #4269是坐标系GCS_North_American_1983的工厂代码
arcpy.CreateFeatureClass_management(out_FC_path,out_FC_name,geometry_type,spatial_reference=spatial_reference)


# 添加字段
arcpy.env.workspace = "D:\EX08\data\EX08.gdb"

in_table = "MapXY"
field1_name = "Longtitude"
field1_type = "DOUBLE"
field2_name = "Latitude"
filed2_type = "DOUBLE"
arcpy.AddFiled(in_table,field1_name,field1_type) #添加经度字段
arcpy.AddFiled(in_table,field2_name,field2_type) #添加纬度字段


# 使用InsertCursor将文本文件中的坐标信息导入到FeatureClass表中
txtFile_path = "D:/EX08/data/textToFC.txt"

Cursor = arcpy.InsertCursor("MapXY") #"D:\EX08\data\EX08.gdb\MapXY"
TxtFile = open(txtFile_path,'r')
for line in TxtFile:
    p = line.split(',')
    row = Cursor.newRow()
    #Add the Point geometry to the row
    Point = arcpy.CreateObject("Point")
    Point.X = p[0]
    Point.Y = p[1]
    row.shape = Point
    #Add attributes
    row.Longtitude = p[0]
    row.Latitude = p[1]
    #write to the FeatureClass
    Cursor.insertRow(row)
del Cursor


#MapXY.shp 与 usa.mxd中的State图层进行 空间连接(SpatialJoin)操作,得到Join_MapXY.shp
#arcpy.env.workspace = "D:\EX08\data\EX08.gdb"

target_features = "MapXY"   # "D:\EX08\data\EX08.gdb\MapXY"
join_features = "States"
out_features_class = "Join_MapXY"
arcpy.SpatialJoin_analysis(target_features,join_features,out_features_class)


#在这里向MapXY插入字段"State_Name"(之所以没有在前面就插入字段"State_Name",是为了避免Join时_1字段的出现)
#arcpy.env.workspace = "D:\EX08\data\EX08.gdb"

#in_table = "MapXY"
field3_name = "State_Name"
field3_type = "TEXT"
field3_length = 25
arcpy.AddFiled(in_table,field3_name,field3_type,field3_length)


#然后用UpdateCursor,用Join_MapXY.shp中“STATE_NAME”字段的值 来更新MapXY.shp的“State_Name”字段
#arcpy.env.workspace = "D:\EX08\data\EX08.gdb"

Cur = arcpy.UpdateCursor("MapXY")  # "D:\EX08\data\EX08.gdb\MapXY"
for row in Cur:
    Cur1 = arcpy.SearchCursor("Join_MapXY")  # "D:\EX08\data\EX08.gdb\Join_MapXY"
    for row1 in Cursor1:
        value = row1.getValue("STATE_NAME")
        row.setValue("State_Name",value)
        Cur.updateRow(row)
        row = Cur.next()
del row
del Cur

发布了44 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/IT_xiao_guang_guang/article/details/103300998