@@ -0,0 +1,939 @@
+import numpy as np%0Aimport fiona%0Afrom shapely.geometry import Point, asShape%0Afrom shapely.prepared import prep%0Afrom shapely import speedups%0A%0A%0Adef append_nuts3_region(dfin, shapefile):%0A    %22%22%22 Take a pandas dataframe with lat and long columns and a shapefile.%0A    Convert coordinates to Shapely points to do a point-in-polygon check%0A    for each point. Append name of nuts3 region corresponding to point-in-polygon%0A    to the dataframe.%0A    This is extremely slow. Needs to be optimized with Shapely boundary box.%0A    %22%22%22%0A    df = dfin.copy()%0A    df%5B'nuts3'%5D = np.nan%0A    fc = fiona.open(shapefile)%0A    speedups.enable()%0A    for feature in fc:%0A        prepared_shape = prep(asShape(feature%5B'geometry'%5D))%0A        for index, row in df.iterrows():%0A            point = Point(row.loc%5B'long'%5D, row.loc%5B'lat'%5D)%0A            if prepared_shape.contains(point):%0A                df.loc%5Bindex, 'nuts3'%5D = feature%5B'properties'%5D%5B'NUTS315NM'%5D%0A    return df%0A

add nuts3 region to df; needs major optimization