-2

I have a list of customers lat and long and I want to define which ones are within given a list of zones or polygons.

from shapely.geometry import Polygon
from shapely.geometry import Point
import pandas as pd
from shapely.wkt import loads
import geopandas as gpd
df=pd.read_csv("C:\\Users\\n.nguyen.2\\Documents\\order from May 1.csv")

geometry=gpd.points_from_xy (df['customer_lng'],df['customer_lat'])
crs={'init':'epsg:4326'}
gdf=gpd.GeoDataFrame(df,crs=crs,geometry=geometry)
gdf.head()

far_east= Polygon ([(103.91805, 1.3167), (103.92062, 1.31086), (103.92491, 1.29859), (103.96208, 1.31395), (104.00447845458986, 1.30966224489856), (104.02645111083986, 1.32819676167883), (104.04258728027344, 1.36217634666417), (104.01769638061523, 1.37968079509861), (103.997097, 1.390149), (103.976498, 1.392208), (103.96362304687501, 1.39564062116584), (103.95177841186523, 1.39804316627834), (103.9406204223633, 1.40044570893216), (103.93529891967773, 1.40027409882423), (103.92869, 1.4007), (103.91908, 1.39375), (103.91436, 1.38612), (103.9183, 1.3814), (103.9244, 1.36793), (103.92199, 1.35428), (103.92054, 1.35338), (103.92024, 1.35304), (103.9186, 1.35201), (103.9183, 1.35064), (103.915, 1.34793), (103.91472, 1.34776), (103.91459, 1.34755), (103.91439, 1.34739), (103.91427, 1.34726), (103.91393, 1.34697), (103.9126, 1.34631), (103.91144, 1.34604), (103.89942, 1.34278), (103.89629, 1.33802), (103.90217, 1.33223), (103.9074, 1.32631), (103.91092, 1.32369), (103.91406, 1.32198), (103.91422, 1.32105), (103.91419, 1.32039), (103.91805, 1.3167)])
east= Polygon  ([(103.91324043273929, 1.34664533115595), (103.89742610000008, 1.342355), (103.8942289352417, 1.34304143652896), (103.89186870000005, 1.3436426), (103.88508790000004, 1.3433418), (103.8831514120102, 1.34190717353805), (103.88207584619522, 1.34111345727885), (103.88165479999998, 1.3405959), (103.88049460000002, 1.3391478), (103.8774062, 1.3351903), (103.87541110000006, 1.3335226), (103.8722037, 1.3307341), (103.8684495, 1.3293188), (103.86686340000006, 1.3288479), (103.86384499999996, 1.3287118), (103.8668917, 1.3247646), (103.86723499999994, 1.319101), (103.86948800000005, 1.31554), (103.86908459999997, 1.3138004), (103.87028199999997, 1.309555), (103.86877959999993, 1.3053288), (103.86925210000004, 1.303312), (103.86534699999991, 1.299794), (103.86504660000001, 1.2968769), (103.86579759999996, 1.2926719), (103.87560780000003, 1.2777607), (103.89663629999995, 1.2938928), (103.9396375, 1.3075793), (103.94551690000002, 1.3080083), (103.96068739999998, 1.3143152), (103.95594519999997, 1.3204505), (103.95199709999999, 1.3244406), (103.94869259999996, 1.3283019), (103.94349979999993, 1.3313052), (103.93852170000002, 1.334952), (103.931784, 1.3418166), (103.91669920000004, 1.3483594), (103.91564369201662, 1.34879050707076), (103.91324043273929, 1.34664533115595)])
island= Polygon ([(103.74612808227538,1.2686455612039458),(103.72690200805664,1.292500533024804),(103.69342803955078,1.2964477370816825),(103.6805534362793,1.26675774823251),(103.69394302368164,1.2293444464408747),(103.72793197631836,1.2485660257767572),(103.74612808227538,1.2686455612039458)])

for row in gdf:
    if gdf['geometry'].within(far_east):
        gdf['answer']='Far_east'
    elif gdf['geometry'].within(east):
        gdf['answer']='East'
    else:
        gdf['answer']='Jurong_Island'                 

writer = pd.ExcelWriter("C:\\Users\\n.nguyen.2\\Documents\\order may define2.xlsx")
gdf.to_excel(writer, 'Sheet1', index=False)
writer.save()

I got an error like this:

ValueError                                Traceback (most recent call last)
<ipython-input-8-44e2be7f9d4b> in <module>
     16 
     17 for row in gdf:
    ---> 18     if gdf['geometry'].within(far_east)
     19         gdf['answer']='Far_east'
     20     elif gdf['geometry'].within(east):
    ~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
    1476         raise ValueError("The truth value of a {0} is ambiguous. "
    1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
    -> 1478                          .format(self.__class__.__name__))
    1479 
    1480     __bool__ = __nonzero__
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Raw data:

enter image description here

Georgy
  • 6,348
  • 7
  • 46
  • 58

1 Answers1

1

Seems like the following code mistakenly uses gdf where it should use row

for row in gdf:
    if gdf['geometry'].within(far_east):
        gdf['answer']='Far_east'
    elif gdf['geometry'].within(east):
        gdf['answer']='East'
    else:
        gdf['answer']='Jurong_Island'   

should be

for idx,row in gdf.iterrows():
    if row['geometry'].within(far_east):
        row['answer']='Far_east'
    elif row['geometry'].within(east):
        row['answer']='East'
    else:
        row['answer']='Jurong_Island'

other wise the for loop makes no sense. You get the error because only some rows may lay within region but you testing for all rows at ones by the use of gdf. Using row will only check one row and than result is clear. But be aware that the writeback might not work.

Lee
  • 1,293
  • 8
  • 17
  • Thanks for the response! But I got another issue as: ```Python Traceback (most recent call last): File "", line 18, in if row['geometry'].within(fareast): TypeError: string indices must be integers ``` – Ngoc Nguyen Jun 11 '19 at 07:37
  • Ok found out you also missing iterrows and now it get complicated. While this might work it is not the way pandas should be used. – Lee Jun 11 '19 at 09:57
  • Then how should I correct it? Could you please help? – Ngoc Nguyen Jun 11 '19 at 11:57
  • 2
    Could you post the example raw_data as actual text not picture – Lee Jun 11 '19 at 12:02
  • Hi @Lee, now the error is ```Python File "", line 27 elif row['geometry'].within(jurong_west): ^ IndentationError: unindent does not match any outer indentation level ``` – Ngoc Nguyen Jun 12 '19 at 01:51
  • Now the script works but there is no column as "answer". – Ngoc Nguyen Jun 12 '19 at 02:42
  • can you please help? Here is an example of raw data customer_lat customer_lng 1.442622 103.7917604 1.354673 103.947203 1.3389925 103.9510167 1.2736764 103.8348949 1.3375437 103.931517 1.3576255 103.9638646 1.298484 103.800959 1.3702234 103.8822774 1.3740792 103.8855366 1.3892897 103.9037255 Thanks! – Ngoc Nguyen Jun 16 '19 at 08:16