<jupyter_start><jupyter_text>Read the data framesingle list of lat long from Jan<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline

import shapely
from shapely.geometry import Point # Point class
from shapely.geometry import shape # shape() is a function to convert geo objects through the interface
import shapefile
import pandas as pd
import numpy as np

#LongLat = pd.read_pickle('LongLat.pkl')
LongLat = pd.read_pickle('LongLat_UTM.pkl')
# https://chrishavlin.com/2016/11/16/shapefiles-tutorial/
shp = shapefile.Reader('shapefiles/taxi_zones.shp') #open the shapefile
print ('number of shapes imported: %s' % len(shp.shapes()))
# https://pypi.org/project/pyshp/#reading-records
shp.shapeType
# POLYGON = 5

shp.bbox
shp.shape(7)
shp.fields
shape_ex = shp.shape(5)
x_lon = np.zeros((len(shape_ex.points),1))
y_lat = np.zeros((len(shape_ex.points),1))
for ip in range(len(shape_ex.points)):
    x_lon[ip] = shape_ex.points[ip][0]
    y_lat[ip] = shape_ex.points[ip][1]
plt.plot(x_lon,y_lat,'k')

# use bbox (bounding box) to set plot limits
plt.xlim(shape_ex.bbox[0],shape_ex.bbox[2])
all_shapes = shp.shapes() # get all the polygons
all_records = shp.records()

len(all_shapes)
print(all_records)
x = LongLat['x'][0]
y = LongLat['y'][0]
print(x,y)
print(Point(x,y))
for i in range(0, len(all_shapes)): # len(all_shapes)
        boundary = all_shapes[i] # get a boundary polygon
        print(boundary)
        if Point(x,y).within(shape(boundary)): # make a point and see if it's in the polygon
            name = all_records[i][3] # get the fourth field of the corresponding record
            print ("The point is in", name  )
            
    #name = all_records[i][3] # get the fourth field of the corresponding record
    #print ("The point is not in", name  )          
for row in range(0, 1000):
    point = LongLat['LaLoPoint'][row]
    for i in range(0, len(all_shapes)): # len(all_shapes)
        boundary = all_shapes[i] # get a boundary polygon
        #print(boundary)
        if Point(x,y).within(shape(boundary)): # make a point and see if it's in the polygon
            name = all_records[i][3] # get the fourth field of the corresponding record
            print ("The point is in", name  )
            
    #name = all_records[i][3] # get the fourth field of the corresponding record
    #print ("The point is not in", name  )<jupyter_output><empty_output><jupyter_text>Location Data<jupyter_code>def get_lat_lon(sf):
    content = []
    for sr in sf.shapeRecords():
        shape = sr.shape
        rec = sr.record
        loc_id = rec[shp_dic['LocationID']]
        
        x = (shape.bbox[0]+shape.bbox[2])/2
        y = (shape.bbox[1]+shape.bbox[3])/2 
        content.append((loc_id, x, y))
    return pd.DataFrame(content, columns=["LocationID", "longitude", "latitude"])
sf = shapefile.Reader("shapefiles/taxi_zones.shp")
sf
fields_name = [field[0] for field in sf.fields[1:]]
fields_name
shp_dic = dict(zip(fields_name, list(range(len(fields_name)))))
shp_dic
attributes = sf.records()
attributes
shp_attr = [dict(zip(fields_name, attr)) for attr in attributes]
shp_attr

df_loc = pd.DataFrame(shp_attr).join(get_lat_lon(sf).set_index("LocationID"), on="LocationID")
df_loc.head()<jupyter_output><empty_output><jupyter_text>Try to find the closest<jupyter_code>from math import cos, asin, sqrt

def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
    return 12742 * asin(sqrt(a))

def closest(data, v):
    return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon']))


tempDataList = [{'lat': 39.7612992, 'lon': -86.1519681}, 
                {'lat': 39.762241,  'lon': -86.158436 }, 
                {'lat': 39.7622292, 'lon': -86.1578917}]


v = {'lat': 39.7622290, 'lon': -86.1519750}
print(closest(tempDataList, v))<jupyter_output>{'lat': 39.7612992, 'lon': -86.1519681}
{'fraction_non_alphanumeric': 0.1476033477047933, 'fraction_numerical': 0.04184630991630738, 'mean_word_length': 3.923845193508115, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '440250', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1446, 'n_words': 346, 'path': 'Presented Code/Spatial Server/Generate location.ipynb', 'repo_name': 'RoryIAngus/Elula', 'size': '3943'}