I have two table, one is presenting all countries and their respecting polygons & multipolygons.
Another one is presenting latitude and longitude Points() (dist_base).
My goal is here to "join" my point data to their respecting countries. I'm on Pyspark and I can't use geopandas, I'm then using shapely but the way I'm implementing it is really slow.
Do you have any advice using shapely?
(the idea would be to check in which polygoin my point is located).
What I tried is to check polygon after polygon (stored in geodata_countries_geo), if the point is inside, if yes, retrieve the ISO3 code associated (stored in a dictionnary geodata_countries_geo_dict)
import shapely.wkt as wkt
def check_nearest_country(lat, lon):
point = Point(lon, lat)
for geometry in geodata_countries_geo:
polygon = wkt.loads(geometry[0])
if polygon.contains(point):
return str(geodata_countries_geo_dict.get(str(geometry[0])))
# register udf
check_nearest_country_udf = f.udf(lambda x,y: str(check_nearest_country(x,y)), StringType())
# apply
dist_base = dist_base.withColumn('Country_Base_ISO3', check_nearest_country_udf(f.col("lat"),f.col("lon")))