可以在形状文件中搜索记录,但如何获取其他字段

2024-10-06 06:46:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在一个shapefile中搜索一个属性,它工作得很好,但我不知道在找到正确的记录后如何获取该记录中的其他字段。不知道我应该使用SearchCursor还是SelectLayerByAttribute_管理

townlands = r'F:\MyProject\Assignment\townlands.shp'
outpath = r'F:\MyProject\Assignment'
the_townland=str(text_search_townland.get())
selection = str(""" "NAME_TAG" = '""" + the_townland + "'")
selection2 = ????????????????
print selection, selection2

这段代码的工作原理是,它查找用户输入文本\u search\u townland的城镇,并将其打印为选择。我希望从该记录中获取另一个名为OSM_USER的字段,并将其输入selection2


Tags: thesearch属性myproject记录shapefileassignmentshp
1条回答
网友
1楼 · 发布于 2024-10-06 06:46:26

经过多次尝试和错误后,我终于成功了。它确实需要SearchCursor,至少我是这样让它工作的

def new_record():

    #set environment variables.
    arcpy.env.workspace = r'F:\MyProject\Assignment\folklore.gdb'
    myPath = r'F:\MyProject\Assignment\folklore.gdb'
    editRows = arcpy.da.InsertCursor('folklore', '*')
    print editRows.fields

    # get the centroid of the townland from townland_centroid (fc) based on the
    # townland the user enters.
    database = r'F:\MyProject\Assignment\folklore.gdb'
    fc = database + '/' + 'townland_centroid'

    the_townland=str(text_search_townland.get())
    fields = ['NAME_TAG', 'X_coord', 'Y_coord']
    whereClause = '"NAME_TAG"' + " = '" + the_townland + "'"

    with arcpy.da.SearchCursor(fc, fields, whereClause) as cursor:
        for row in cursor:
            print('{0}, {1}, {2}'.format(row[0], row[1], row[2]))
    X_coord = str(row[1])
    Y_coord = str(row[2])
    del cursor

    # Set variables with values that will populate 'folklore' featureclass.
    OID = 1
    ptShape = arcpy.Point(0,0)
    townland = text_search_townland.get()
    county = var_county2.get()
    category = var_category.get()
    URL = text_search_URL.get()
    spec_location = "text_search_speclocation.get()"
    date_entered = text_search_date_entered.get()
    story_year = int(text_search_story_year.get())
    X_coord_put = X_coord
    Y_coord_put = Y_coord
    newRecord = [OID, ptShape, townland, county, URL, spec_location, date_entered, story_year, category, X_coord, Y_coord]
    editRows.insertRow(newRecord)
    del editRows

希望这对别人有帮助

相关问题 更多 >