예제 #1
0
파일: test_here.py 프로젝트: SavOK/YelpTime
def get_data(point):
    HA = HereAPI()
    s = Session()
    r = HA.get_isoline(point[0], point[1])
    isoline = ",".join(get_isoline_generator(r))
    q = query_points_within(isoline, s)
    # return q
    # Q = get_data((40.8014, -73.9501))
    loc_list = list(generate_dict_from_query(q))

    r_m = HA.get_route_matrix(loc_list, point)

    dist_mat = r_m.json()
    # if 'response' not in dist_mat:
    #     return None
    mat_list = sorted(
        [
            x for x in dist_mat["response"]["matrixEntry"]
            if x["destinationIndex"] == 0
        ],
        key=itemgetter("startIndex"),
    )
    for mat, loc in zip(mat_list, loc_list):
        loc.update({
            "travelTime": mat["summary"]["travelTime"],
            "routeDistance": mat["summary"]["distance"],
        })

    df = pd.DataFrame(loc_list)
    # print(
    #     df.sort_values(by=["travelTime", "routeDistance"]).reset_index(drop=True)[:10]
    # )
    return df
예제 #2
0
파일: test_here.py 프로젝트: SavOK/YelpTime
def query_points_within(isoline: str, s: Session):
    poligon = "POLYGON(({}))".format(isoline)
    q = (s.query(Business).join(
        Location, Business.location_id == Location.id).join(
            Category, Business.categories).filter(
                Category.category_alias == "pizza").filter(
                    ST_Within(Location.location,
                              poligon)).filter(Business.price != None))
    return q
예제 #3
0
def add_data_to_table(in_data: Dict):
    s = Session()
    for D in in_data["businesses"]:
        if D["coordinates"]["latitude"] is None:
            continue
        business = Business(D)
        business.source_db = "Yelp_API"
        business, isNewBus = business.get_or_create(s)
        for A in D["categories"]:
            category = Category(entry=A)
            category, isNewCat = category.get_or_create(s)
            business.categories.append(category)
        location = Location(D["coordinates"]["latitude"],
                            D["coordinates"]["longitude"])
        location, isNewLoc = location.get_or_create(s)
        business.location = location
        s.commit()
    s.close()
예제 #4
0
                "url": None,
                "source_db": "NY_License",
                "categories": [
                    {
                        "alias": row["Industry"].lower().replace(" ", ""),
                        "title": row["Industry"],
                    }
                ],
            }
    return all_data


if __name__ == "__main__":
    data_file = Path(config.DATA_LOC) / "Legally_Operating_Businesses.csv"
    all_data = get_all_data(data_file)
    s = Session()
    for B in all_data.values():
        B["is_closed"] = False
        business = Business(B)
        business.source_db = "NY_License"
        business, isNewBus = business.get_or_create(s)
        for A in B["categories"]:
            category = Category(entry=A)
            category, isNewCat = category.get_or_create(s)
            business.categories.append(category)
        location = Location(B["latitude"], B["longitude"])
        location, isNewLoc = location.get_or_create(s)
        business.location = location
        s.commit()
    s.close()
예제 #5
0
def get_data_around_point(
    centerPoint: Tuple, rangePar: int, transportMode: str, busnessType: str
) -> pd.DataFrame:
    """
    Get DataFrame of the points around point within limit
    """
    isoline = get_isoline(centerPoint, rangePar, transportMode)
    s = Session()
    c = s.connection()
    stm = """
    WITH SUBT AS (
        SELECT *, ST_MakePoint(B.latitude, B.longitude) as Point 
            FROM main_table AS B
            WHERE B.naics_code='{0}'
    )
    SELECT B.name, B.address, B.city, B.state, B.zip_code, B.latitude, B.longitude
        FROM SUBT AS B 
        WHERE ST_Within(B.Point, 'POLYGON(({1}))')
        ORDER BY
            B.Point <-> 'POINT({2} {3})'
        LIMIT 100;
    """.format(
        busnessType, isoline, centerPoint[0], centerPoint[1]
    )
    r = c.execute(stm)
    data = list(read_query(r))
    s.close()

    if len(data) == 0:
        print(f"Nothing found around {centerPoint}")
        return

    HA = HereAPI()
    if transportMode == "car":
        rangeSear = rangePar * 200
    else:
        rangeSear = rangePar * 10
    r = HA.get_route_matrix(
        loc_list=data,
        point=centerPoint,
        transportModeType=transportMode,
        searchRange=rangeSear,
    )
    if r.status_code != 200:
        print("Something wrong with matrix request")
        l = json.dumps(r.json(), indent=1)
        pprint(l)
    dist_data = r.json()
    dist_matrix = sorted(
        dist_data["response"]["matrixEntry"], key=itemgetter("destinationIndex")
    )
    if len(dist_matrix) == 0:
        pprint(r.json())
    if "summary" not in dist_matrix[0]:
        pprint(r.json())
    for ix, dist in enumerate(dist_matrix):
        data[ix].update(
            {
                "distance": dist["summary"]["distance"],
                "travel": dist["summary"]["travelTime"],
            }
        )
    return pd.DataFrame(data)