import logging import sys from goojia_utils import ElasticSearchClient, commutils import pandas as pd from elasticsearch import helpers import transbigdata as tbd log = logging.getLogger(__name__) city_name = sys.argv[1] if len(sys.argv) > 1 else None if pd.notna(city_name): log.info("同步城市:" + city_name) es206_client = ElasticSearchClient(host='192.168.0.206') # 中国地理位置边界[lon1,lat1,lon2,lat2] (WGS84坐标系) 其中,lon1,lat1是左下角坐标,lon2,lat2是右上角坐标, china_bounds = [73.66, 3.86, 135.05, 53.55] # 栅格大小(米) accuracy = 1000 # 生成栅格参数 params_rect = tbd.area_to_params(china_bounds, accuracy=accuracy) # def find_comm_by_grid(city, geo_shape): # comm_info_df = es206_client.scan_to_data_frame('community_summary', { # "_source": ["id", "city", "region", "name", "estate_type"], # "query": { # "bool": { # "filter": [ # { # "term": { # "city": { # "value": city # } # } # }, # { # "geo_polygon": { # "location": { # "points": geo_shape # } # } # } # ] # } # } # }) # return comm_info_df estate_type_list = ["普通住宅", "别墅", "商住"] def save_to_community_summary(comm_df): actions = list() count = 0 for index, item in comm_df.iterrows(): if pd.isna(item['grid_id']): continue doc_info = {'grid_id': item['grid_id']} action = { "_op_type": "update", "_index": "community_summary", "_id": item['_id'], "doc": doc_info, "_retry_on_conflict": 5 } actions.append(action) if len(actions) == 5000: helpers.bulk(es206_client.elastic_client, actions) count += len(actions) log.info("更新:" + str(count)) actions.clear() if len(actions) > 0: helpers.bulk(es206_client.elastic_client, actions) actions.clear() def sync(city): sql = f"select * from grid " if pd.notna(city): sql += f" where city='{city}' " origin_grid_df = es206_client.scan_by_sql_to_data_frame(sql) cities = origin_grid_df['city'].unique() for curr_city in cities: grid_df = origin_grid_df[origin_grid_df['city'] == curr_city].copy() avg_price_df = commutils.get_avg_price(curr_city) if len(avg_price_df) <= 0: logging.info(curr_city + "无价格信息") continue avg_price_df = avg_price_df[['city', 'region', 'district_name', 'avg_price']] avg_price_df = avg_price_df.rename(columns={'district_name': 'name'}) comm_df = es206_client.scan_by_sql_to_data_frame(f"select id, city, region, name, estate_type, location from community_summary where city='{curr_city}' ") def deal_lng_lat(x): location = x['location'] if pd.notna(location): lon_lat_col = tbd.GPS_to_grid(location['lon'], location['lat'], params_rect) x['grid_id'] = str(lon_lat_col[0]) + "_" + str(lon_lat_col[1]) return x comm_df = comm_df.apply(lambda x: deal_lng_lat(x), axis=1) # 格子信息写入community_summary save_to_community_summary(comm_df) # 求解格子均价 actions = list() count = 0 log.info(f"开始计算{curr_city}格子价格") for index, item in grid_df.iterrows(): comm_info_df = comm_df[comm_df['grid_id'] == item['_id']] if len(comm_info_df) <= 0: continue grid_info = dict() price_list = list() for estate_type in estate_type_list: # 计算不同物业类型的小区均价 comm_info_estate_type_df = comm_info_df[comm_info_df['estate_type'] == estate_type].copy() if len(comm_info_estate_type_df) > 0: comm_info_estate_type_df = pd.merge(comm_info_estate_type_df, avg_price_df, how='left', on=['city', 'region', 'name']) comm_info_estate_type_df = comm_info_estate_type_df[comm_info_estate_type_df['avg_price'].notna()] if len(comm_info_estate_type_df) > 0: price_info = dict() price_info['type'] = 'district' price_info['secondary_type'] = estate_type price_info['price'] = int(comm_info_estate_type_df['avg_price'].mean()) price_list.append(price_info) if len(price_list) > 0: grid_info['price'] = price_list action = { "_op_type": "update", "_index": "grid", "_id": item['_id'], "doc": grid_info, "_retry_on_conflict": 5 } actions.append(action) if len(actions) == 5000: helpers.bulk(es206_client.elastic_client, actions) count += len(actions) log.info("更新格子数:" + str(count)) actions.clear() if len(actions) > 0: helpers.bulk(es206_client.elastic_client, actions) count += len(actions) log.info("更新格子数:" + str(count)) actions.clear() log.info(f"完成计算{curr_city}格子价格") sync(city_name)