import pandas as pd import geopandas as gpd from pyogrio import read_dataframe, write_dataframe from shapely.geometry import Point grid_df = read_dataframe('DATA/refined/geopackage/시군구별/중분류_37100_경산시.gpkg', encoding='utf-8') light_df = pd.read_csv("DATA/raw/보안등/경상북도_경산시_보안등정보_20230711.csv", encoding="ms949") grid_df = grid_df.to_crs("epsg:4326") def is_float(x): try: float(x) return True except ValueError: return False valid_lightings_df = light_df[light_df['경도'].apply(is_float) & light_df['위도'].apply(is_float)] valid_lightings_df['geometry'] = valid_lightings_df.apply(lambda row: Point(float(row['경도']), float(row['위도'])), axis=1) valid_lightings_df = gpd.GeoDataFrame(valid_lightings_df, geometry='geometry') buffer_radius = 50 points_buffered = valid_lightings_df.buffer(buffer_radius) valid_lightings_df = points_buffered def calculate_intersection_ratio(polygon, points_buffered): intersections = points_buffered.intersection(polygon) intersection_area = intersections.area.sum() return intersection_area / polygon.area # Apply the function to each polygon grid_df['intersection_ratio'] = grid_df.apply( lambda row: calculate_intersection_ratio(row['geometry'], points_buffered), axis=1 ) write_dataframe(grid_df, "감시비율(100m).gpkg")