尝试使用 for 循环迭代数据帧
作为我的第一个项目,我正在开发一个程序,该程序接收一个地址并返回该地址所在的政治区。我已经编写了地理编码函数,现在我正在开发一个接受长/纬度作为参数的函数,迭代形状文件中的多边形。如果它找到包含这些坐标的多边形,则返回与该多边形关联的地区名称。
我已经编写了该函数的一个版本,它可以工作,但它要求我指定(使用 loc 方法)要检查哪个多边形。如果坐标位于该多边形内,则返回关联区域的名称。否则,返回“否”。这个函数的代码如下:
def senate_function(long, lat): #takes in long and lat coordinates as arguments
coordinates_1 = Point(long, lat) #converts coordinates to a point using Point method
polygon = (s_districts.loc[3]['geometry']) #specifies which polygon to look in
if coordinates_1.within(polygon) == True: #prints True if coordinates are in specified polygon
print(s_districts['NAMELSAD20'][3])
else: print("No")
我已经在几个地址上测试了它并确认它确实有效,所以我的下一步是编写这个函数的更好版本,它将迭代行,找到坐标所在的多边形位于其中,并返回关联的地区名称。这就是我写的:
def senate_function_1(long, lat): #takes in long and lat coordinates as arguments
coordinates_2 = Point(long, lat) #converts coordinates to a point using Point method
for row in s_districts["geometry"]:#loops through the column that contains polygons
if coordinates_2.within == True: #checks to see if points is within the polygon
print(s_districts['NAMELSAD20']) #if true, prints the district name corresponding to the polygon
else: print('False')
当我调用这个函数时,它会为每一行返回“False”。因此它正在迭代文件,但由于某种原因,它没有检查坐标是否在多边形内。我对编码很陌生,如果我搞砸了术语,很抱歉,但我想尽可能详细。谢谢你!
As one of my first projects, I'm working on a program that takes in an address and returns which political districts that address is located within. I've written the geocoding function, now I'm working on a function that accepts long/lat as arguments, iterates through the polygons in the shapefile. If it finds a polygon that contains those coordinates, returns the district name associated with that polygon.
I've written a version of this function that works, but it requires me to specify (using the loc method) which polygon to check. If coordinates are within that polygon, it returns the name of the associated district. Otherwise, it returns "no". Here's what the code looks like for this function:
def senate_function(long, lat): #takes in long and lat coordinates as arguments
coordinates_1 = Point(long, lat) #converts coordinates to a point using Point method
polygon = (s_districts.loc[3]['geometry']) #specifies which polygon to look in
if coordinates_1.within(polygon) == True: #prints True if coordinates are in specified polygon
print(s_districts['NAMELSAD20'][3])
else: print("No")
I've tested it on several addresses and confirmed it does work, so my next step is to write a better version of this function that will iterate through the rows, find the polygon the coordinates are located within, and return the associated district name. This is what I wrote:
def senate_function_1(long, lat): #takes in long and lat coordinates as arguments
coordinates_2 = Point(long, lat) #converts coordinates to a point using Point method
for row in s_districts["geometry"]:#loops through the column that contains polygons
if coordinates_2.within == True: #checks to see if points is within the polygon
print(s_districts['NAMELSAD20']) #if true, prints the district name corresponding to the polygon
else: print('False')
When I call this function it returns "False" back for every row. So it is iterating through the file, but for some reason, it's not checking to see if the coordinates are within the polygon. I'm very new to coding so sorry if I've screwed up terminology, but I wanted to be as detailed as possible. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的描述,您可能忘记在第二个代码片段中指定 inside 方法的参数。您还遗漏了要打印哪个地区名称的规范。目前它总是会打印整个系列。我想你可能想要类似下面的东西:
Based on your description, you may have forgotten to specify the argument of the within method in your second snippet. You also left out the specification of which district name to print. Currently it would always print out the whole series. I think you probably wanted something like the following: