Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Filter Shorelines within a Polygon #232

Closed
3 tasks done
2320sharon opened this issue Mar 1, 2024 · 8 comments
Closed
3 tasks done

Feature Request: Filter Shorelines within a Polygon #232

2320sharon opened this issue Mar 1, 2024 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@2320sharon
Copy link
Collaborator

2320sharon commented Mar 1, 2024

Shoreline Filtering

image (16)

We want to implement improve outlier remove for our extracted shorelines. Currently shorelines a filtered by removing any detected shoreline points that lie outside of the reference shoreline buffer. This new technique would have the user draw a polygon that would define where extracted shorelines could be extracted then extract all the shorelines and then filter the extracted shorelines to keep only the shorelines that exist within the polygon.

If this technique were implemented first the points outside reference shoreline buffer would be filtered out, then more points would be filtered out if they were outside the polygon.

Key Elements of this solution

  • Applied after shoreline extraction from images
  • Uses the shorelines gdf that are saved as LineStrings
  • Maintains the order of points when applying filter to each linestring segment.

Tasks

  • Verify this technique works on complex shorelines
  • Verify this technique works on back barrier shorelines
  • Verify the code to preserve extracted shorelines attributes

Credit @mlundine for the figure and code.

import numpy as np
import os
import geopandas as gpd
import shapely
from shapely import geometry

def arr_to_LineString(coords):
    """
    Makes a line feature from a list of xy tuples
    inputs: coords
    outputs: line
    """
    points = [None]*len(coords)
    i=0
    for xy in coords:
        points[i] = shapely.geometry.Point(xy)
        i=i+1
    line = shapely.geometry.LineString(points)
    return line

def LineString_to_arr(line):
    """
    Makes an array from linestring
    inputs: line
    outputs: array of xy tuples
    """
    listarray = []
    for pp in line.coords:
        listarray.append(pp)
    nparray = np.array(listarray)
    return nparray



def ref_poly_filter(reference_polygon, model_shorelines):
    """python
    filters extracted shorelines that are not contained within a reference region/polygon
    saves output with '_ref_poly_filter' appended to original filename in same directory
    inputs:
    reference_region_path (str): path to reference region geojson
    model_shorelines (str): path to extracted shorelines
    outputs:
    save_path (str): path to the output file
    """

    ##Loading stuff in
    save_path = os.path.splitext(model_shorelines)[0]+'_ref_poly_filter.geojson'
    model_gdf = gpd.read_file(model_shorelines)
    ref_poly_gdf = gpd.read_file(reference_polygon)

    ##First need to get rid of lines that are completely outside of the ref polygon
    ref_polygon = ref_poly_gdf.geometry
    buffer_vals = [None]*len(model_gdf)
    for i in range(len(model_gdf)):
        line_entry = model_gdf.iloc[i]
        line = line_entry.geometry
        bool_val = ref_polygon.intersects(line).values[0]
        buffer_vals[i] = bool_val
    model_gdf['buffer_vals'] = buffer_vals
    model_gdf_filter = model_gdf[model_gdf['buffer_vals']]

    ##Now get rid of points that lie outside ref polygon but preserve the rest of the shoreline
    new_lines = [None]*len(model_gdf_filter)
    for i in range(len(model_gdf_filter)):
        line_entry = model_gdf_filter.iloc[i]
        line = line_entry.geometry
        line_arr = LineString_to_arr(line)
        bool_vals = [None]*len(line_arr)
        j = 0
        for point in line_arr:
            point = geometry.Point(point)
            bool_val = ref_polygon.contains(point)[0]
            bool_vals[j] = bool_val
            j=j+1
        new_line_arr = line_arr[bool_vals]
        new_line_LineString = arr_to_LineString(new_line_arr)
        new_lines[i] = new_line_LineString

    ##Assign the new geometries, save the output
    model_gdf_filter['geometry'] = new_lines
    model_gdf_filter.to_file(save_path)

    return save_path

@2320sharon 2320sharon added the enhancement New feature or request label Mar 1, 2024
@2320sharon 2320sharon self-assigned this Mar 1, 2024
@2320sharon
Copy link
Collaborator Author

2320sharon commented Mar 1, 2024

If this technique works we will need to make sure these shorelines are used to compute the shoreline transect intersections.

@2320sharon
Copy link
Collaborator Author

Test Case One Gilgo Beach

Original Extracted Shorelines

2023-11-29-15-33-54_L8

image

Filtered Shorelines

  • Red: reference shoreline polygon I drew
  • Green: Filtered Shorelines
  • After filtering there are still 4 remaining shorelines
    image

Source Code and Extracted Shoreline Zip

NY_back_barrier1.zip

keep_shorelines_in_polygon.zip

@mlundine
Copy link

mlundine commented Mar 2, 2024

This method btw preserves the attributes I had made that edit in the slack message to do so.
Just had to reassign geometry to the existing filtered gdf instead of making a new gdf.

@2320sharon
Copy link
Collaborator Author

Here's the latest on the shoreline filtering with the polygon.

  • Named the polygon the user draws shoreline_extraction_area
  • The polygon is optional and can be draw by the user if they select to draw the shoreline extraction area under draw controls
  • The filtering it working great it filters both the geodataframe and the dictionary for all the extracted shorelines
  • Catherine is going to beta test this feature
    Untitled (2)

@2320sharon
Copy link
Collaborator Author

Example of Shoreline Extraction Area

  • shoreline extraction area is shown in purple on this figure
  • in dark blue are the extracted shorelines
  • in black is the reference shoreline
    image (18)

@2320sharon
Copy link
Collaborator Author

2320sharon commented Mar 5, 2024

2023-12-19-16-01-08_L9

I'll need to update the coastsat_package's extract_shoreline function to plot the only the shoreline detected in the shoreline extraction area because if I don't update those code the detection images will not reflect the filtered shorelines as seen above.

I'll update the plotting functions after Catherine verifies it works for her 2 test sites

2320sharon added a commit that referenced this issue Mar 6, 2024
2320sharon added a commit that referenced this issue Mar 6, 2024
2320sharon added a commit that referenced this issue Mar 7, 2024
@2320sharon
Copy link
Collaborator Author

During our meeting today I realized I never had tested the new shorelines extraction area feature with multiple ROI. Good news is it works. The thing is is that if users want to use this feature for multiple ROI they will need to draw a shoreline extraction area for each ROI they have selected not just one. This makes logical sense to me, but what do you think?
image
Screenshot 2024-03-07 113721
image

@2320sharon
Copy link
Collaborator Author

  • Make a wiki tutorial on how to use this feature
  • Plot the shoreline extraction figures with the shoreline extraction area

2320sharon added a commit that referenced this issue Mar 14, 2024
2320sharon added a commit that referenced this issue Mar 15, 2024
2320sharon added a commit that referenced this issue Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants