def road_make(feats, fp): """ Makes road feature df, intersections + non-intersections Args: feats - list of features to be included fp - geojson file for intersections and non intersections Returns: dataframe consisting of features given (if they exist) """ # Read in segments data (geojson) print("reading ", fp) segments = read_geojson(fp) df = pd.DataFrame([x.properties for x in segments]) df.set_index('id', inplace=True) # Check for missing features missing_feats = [x for x in feats if x not in df.columns] feats = [x for x in feats if x in df.columns] if missing_feats: warnings.warn( str(len(missing_feats)) + " feature(s) missing, skipping (" + ', '.join(missing_feats) + ")") return df[feats]
def get_buffer(filename, lat, lon, radius): """ Given a geojson file, latitude and longitude in 4326 projection, and a radius, write to file (as geojson) all the LineStrings and Points that overlap a circular buffer around the coordinates. Args: filename lat lon radius Returns: A list of overlapping geojson features 4326 projection """ segments = read_geojson(filename) # Calculate the bounding circle overlapping = [] point = get_reproject_point(lat, lon, transformer_4326_to_3857) buffered_poly = point.buffer(radius) for segment in segments: if segment.geometry.intersects(buffered_poly): if type(segment.geometry) == LineString: coords = [x for x in segment.geometry.coords] overlapping.append({ 'geometry': { 'coordinates': coords, 'type': 'LineString' }, 'type': 'Feature', 'properties': segment.properties }) elif type(segment.geometry) == Point: overlapping.append({ 'geometry': { 'coordinates': [segment.geometry.x, segment.geometry.y], 'type': 'Point' }, 'properties': segment.properties }) elif type(segment.geometry) == MultiLineString: overlapping.append({ 'geometry': { 'coordinates': [[y for y in x.coords] for x in segment.geometry], 'type': 'MultiLineString' }, 'properties': segment.properties }) else: print("{} not implemented yet, skipping...".format( type(segment.geometry))) if overlapping: overlapping = prepare_geojson(overlapping) return overlapping
def road_make(feats, inters_fp, non_inters_fp, agg='max'): """ Makes road feature df, intersections + non-intersections agg : aggregation type (default is max) IMPORTANT: if the aggregation type changes, need to also update how aggregation is calculated in src/data/add_map.py """ # Read in inters data (json), turn into df with inter index df_index = [] df_records = [] print("reading ", inters_fp) with open(inters_fp, 'r') as f: inters = json.load(f) # Append each index to dataframe for idx, lines in inters.items(): # Each intersection has more than one segment # Add each segment's properties to df_records df_records.extend(lines) df_index.extend([idx] * len(lines)) inters_df = pd.DataFrame(df_records, index=df_index) # Read in non_inters data: print("reading ", non_inters_fp) non_inters = read_geojson(non_inters_fp) non_inters_df = pd.DataFrame([x[1] for x in non_inters]) non_inters_df.set_index('id', inplace=True) # Combine inter + non_inter combined = pd.concat([inters_df, non_inters_df], sort=True) missing_feats = [x for x in feats if x not in combined.columns] feats = [x for x in feats if x in combined.columns] if missing_feats: warnings.warn( str(len(missing_feats)) + " feature(s) missing, skipping (" + ', '.join(missing_feats) + ")") # Since there are multiple segments per intersection, # aggregating inters data = apply aggregation (default is max) aggregated = getattr(combined[feats].groupby(combined.index), agg) # return aggregation and adjacency info (orig_id) return (aggregated(), combined['orig_id'])