Skip to content

Commit

Permalink
remove the shapely dependency and update visual
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Jul 29, 2023
1 parent 8ab98f8 commit 488f882
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 105 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ PyFlowline depends on the following packages
1. `numpy`
2. `gdal`
3. `netCDF4`
4. `shapely`

PyFlowline also has three optional dependency packages

Expand Down
3 changes: 1 addition & 2 deletions conda-recipe/conda_build_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ metadata:
# Package name
name: pyflowline
# Package version
version: "0.2.5"
version: "0.2.6"
# Package summary
summary: A mesh-independent river network generator for hydrologic models.
# Package homepage
Expand All @@ -39,7 +39,6 @@ metadata:
- python >=3.8
- numpy
- gdal
- shapely
- netCDF4
- matplotlib-base
# Package build dependencies
Expand Down
2 changes: 1 addition & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set name = "hexwatershed" %}
{% set git_rev = "main" %}
{% set version = "0.2.5" %}
{% set version = "0.2.6" %}

package:
name: {{ name|lower }}
Expand Down
2 changes: 0 additions & 2 deletions docs/source/installation/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ The following dependency packages will be installed during the process.
* `numpy`
* `gdal`
* `netCDF4`
* `shapely`


=============
Visualization
Expand Down
10 changes: 7 additions & 3 deletions pyflowline/algorithms/auxiliary/calculate_area_of_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
from osgeo import ogr, osr
import importlib

from shapely.ops import polygonize

from pyflowline.algorithms.auxiliary.find_index_in_list import find_list_in_list

from pyflowline.external.pyearth.gis.gdal.gdal_functions import calculate_angle_betwen_vertex_normal
from pyflowline.external.pyearth.gis.gdal.gdal_functions import calculate_polygon_area

Expand All @@ -16,6 +13,13 @@
else:
from pyflowline.algorithms.auxiliary.find_vertex_in_list import find_vertex_in_list

iFlag_shapely = importlib.util.find_spec("shapely")
if iFlag_shapely is not None:
from shapely.ops import polygonize
else:
print('shapely is required for this function')
pass


def calculate_area_of_difference_raw(sFilename_a, sFilename_b):
#not yet supported
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import numpy as np
from osgeo import ogr, osr
from shapely.wkt import loads
#from shapely.wkt import loads
from pyflowline.classes.vertex import pyvertex


import importlib
iFlag_cython = importlib.util.find_spec("cython")
if iFlag_cython is not None:
Expand All @@ -22,7 +21,7 @@ def intersect_flowline_with_flowline( sFilename_flowline_a_in, sFilename_flowlin
if os.path.exists(sFilename_output_in):
os.remove(sFilename_output_in)

pDriver_geojson = ogr.GetDriverByName( "GeoJSON")
pDriver_geojson = ogr.GetDriverByName("GeoJSON")
pDataset_flowline_a = pDriver_geojson.Open(sFilename_flowline_a_in, 0)
pDataset_flowline_b = pDriver_geojson.Open(sFilename_flowline_b_in, 0)
pLayer_flowline_a = pDataset_flowline_a.GetLayer(0)
Expand Down Expand Up @@ -50,9 +49,9 @@ def intersect_flowline_with_flowline( sFilename_flowline_a_in, sFilename_flowlin

pDataset_out = pDriver_geojson.CreateDataSource(sFilename_output_in)

pLayerOut = pDataset_out.CreateLayer('flowline', pSpatial_reference_b, ogr.wkbMultiPoint)
pLayerOut = pDataset_out.CreateLayer('intersect', pSpatial_reference_b, ogr.wkbMultiPoint)
# Add one attribute
pLayerOut.CreateField(ogr.FieldDefn('id', ogr.OFTInteger64)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('pointid', ogr.OFTInteger64)) #long type for high resolution

pLayerDefn = pLayerOut.GetLayerDefn()
pFeatureOut = ogr.Feature(pLayerDefn)
Expand Down Expand Up @@ -126,7 +125,7 @@ def intersect_flowline_with_flowline( sFilename_flowline_a_in, sFilename_flowlin
else:
aVertex_intersect.append(pVertex)
pFeatureOut.SetGeometry(point)
pFeatureOut.SetField("id", lVertexID)
pFeatureOut.SetField("pointid", lVertexID)
pLayerOut.CreateFeature(pFeatureOut)
lVertexID = lVertexID + 1

Expand All @@ -143,7 +142,7 @@ def intersect_flowline_with_flowline( sFilename_flowline_a_in, sFilename_flowlin
else:
aVertex_intersect.append(pVertex)
pFeatureOut.SetGeometry(pGeometry_intersect)
pFeatureOut.SetField("id", lVertexID)
pFeatureOut.SetField("pointid", lVertexID)
pLayerOut.CreateFeature(pFeatureOut)
lVertexID = lVertexID + 1

Expand Down
10 changes: 6 additions & 4 deletions pyflowline/algorithms/intersect/intersect_flowline_with_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import os
import numpy as np
from osgeo import ogr, osr
from shapely.wkt import loads
#from shapely.wkt import loads

from pyflowline.formats.convert_coordinates import convert_gcs_coordinates_to_cell
from pyflowline.formats.convert_coordinates import convert_gcs_coordinates_to_flowline
from pyflowline.external.pyearth.gis.gdal.gdal_functions import get_geometry_coords

def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flowline_in, sFilename_output_in):

Expand Down Expand Up @@ -55,9 +56,10 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
aFlowline_intersect_all=list()
for pFeature_mesh in pLayer_mesh:
pGeometry_mesh = pFeature_mesh.GetGeometryRef()
dummy0 = loads( pGeometry_mesh.ExportToWkt() )
aCoords_gcs = dummy0.exterior.coords
aCoords_gcs= np.array(aCoords_gcs)
#dummy0 = loads( pGeometry_mesh.ExportToWkt() )
#aCoords_gcs = dummy0.exterior.coords
#aCoords_gcs= np.array(aCoords_gcs)
aCoords_gcs = get_geometry_coords(pGeometry_mesh)

lCellID = pFeature_mesh.GetField("cellid")
dLon = pFeature_mesh.GetField("longitude")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@


import os
import numpy as np
from osgeo import ogr, osr
from shapely.wkt import loads
#from shapely.wkt import loads

def intersect_flowline_with_vertex( sFilename_flowline_in, sFilename_vertex_in, sFilename_output_in):

Expand Down Expand Up @@ -41,9 +40,9 @@ def intersect_flowline_with_vertex( sFilename_flowline_in, sFilename_vertex_in,

pDataset_out = pDriver_geojson.CreateDataSource(sFilename_output_in)

pLayerOut = pDataset_out.CreateLayer('flowline', pSpatial_reference_b, ogr.wkbMultiPoint)
pLayerOut = pDataset_out.CreateLayer('intersect', pSpatial_reference_b, ogr.wkbMultiPoint)
# Add one attribute
pLayerOut.CreateField(ogr.FieldDefn('id', ogr.OFTInteger64)) #long type for high resolution
pLayerOut.CreateField(ogr.FieldDefn('pointid', ogr.OFTInteger64)) #long type for high resolution

pLayerDefn = pLayerOut.GetLayerDefn()
pFeatureOut = ogr.Feature(pLayerDefn)
Expand Down
7 changes: 5 additions & 2 deletions pyflowline/classes/_visual_basin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def basin_plot(self,
else:
sFilename_mesh = sFilename_mesh_in

sField_thickness = ''
iFlag_thickness = 0

if iFlag_type_in ==1:
#point based
pass
Expand Down Expand Up @@ -190,6 +189,7 @@ def _plot_polygon_variable(self,
dData_max_in = None,
sFilename_output_in=None,
aExtent_in = None,
aLegend_in = None,
pProjection_map_in = None):
"""_summary_
Expand Down Expand Up @@ -275,10 +275,13 @@ def _plot_polygon_variable(self,
pass

map_vector_polygon_data(sFilename,
iFlag_color_in = 1,
iFlag_colorbar_in = 1,
sFilename_output_in=sFilename_output_in,
sVariable_in= sVariable,
sTitle_in= sTitle,
aExtent_in = aExtent_in,
aLegend_in = aLegend_in,
pProjection_map_in = pProjection_map_in)


Expand Down
8 changes: 4 additions & 4 deletions pyflowline/classes/pycase.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ def mesh_generation(self, iFlag_antarctic_in=None):

if iFlag_mesh_boundary ==1:
#create a polygon based on real boundary
pBoundary_wkt, pBoundary_shp = read_mesh_boundary(self.sFilename_mesh_boundary)
pBoundary_wkt = read_mesh_boundary(self.sFilename_mesh_boundary)

aHexagon = create_hexagon_mesh(iFlag_rotation, dX_lowerleft, dY_lowerleft, dResolution_meter, ncolumn, nrow,
sFilename_mesh, sFilename_spatial_reference, pBoundary_wkt)
Expand All @@ -566,7 +566,7 @@ def mesh_generation(self, iFlag_antarctic_in=None):
nrow= int( (dY_upperleft - dY_lowerleft) / dResolution_meter )
if iFlag_mesh_boundary ==1:
#create a polygon based on real boundary
pBoundary_wkt, pBoundary_shp = read_mesh_boundary(self.sFilename_mesh_boundary)
pBoundary_wkt = read_mesh_boundary(self.sFilename_mesh_boundary)

aSquare = create_square_mesh(dX_lowerleft, dY_lowerleft, dResolution_meter, ncolumn, nrow,
sFilename_mesh, sFilename_spatial_reference, pBoundary_wkt)
Expand All @@ -593,7 +593,7 @@ def mesh_generation(self, iFlag_antarctic_in=None):

if iFlag_mesh_boundary ==1:
#create a polygon based on real boundary
pBoundary_wkt, pBoundary_shp = read_mesh_boundary(self.sFilename_mesh_boundary)
pBoundary_wkt = read_mesh_boundary(self.sFilename_mesh_boundary)
aLatlon = create_latlon_mesh(dLongitude_left, dLatitude_bot, dResolution_degree, ncolumn, nrow,
sFilename_mesh, pBoundary_wkt)
pass
Expand Down Expand Up @@ -634,7 +634,7 @@ def mesh_generation(self, iFlag_antarctic_in=None):
if iFlag_mesh_boundary ==1:
#create a polygon based on
#read boundary
pBoundary_wkt, pBoundary_shp = read_mesh_boundary(self.sFilename_mesh_boundary)
pBoundary_wkt = read_mesh_boundary(self.sFilename_mesh_boundary)

aMpas = create_mpas_mesh(iFlag_global, iFlag_use_mesh_dem, iFlag_save_mesh,
sFilename_mesh_netcdf, sFilename_mesh, iFlag_antarctic_in=iFlag_antarctic_in, pBoundary_in = pBoundary_wkt)
Expand Down
39 changes: 31 additions & 8 deletions pyflowline/external/pyearth/gis/gdal/gdal_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import numpy as np
import osgeo
from osgeo import ogr, osr, gdal
from shapely.wkt import loads
#from shapely.wkt import loads
#most of these functions are copied from the pyearth package



import importlib
iFlag_cython = importlib.util.find_spec("cython")
if iFlag_cython is not None:
Expand Down Expand Up @@ -71,8 +69,6 @@ def calculate_angle_betwen_vertex(x1, y1, x2, y2, x3, y3):
angle3deg = angle_between_vectors_degrees(a3vec, c3vec)
return angle3deg



def angle_between_vectors_degrees(u, v):
"""Return the angle between two vectors in any dimension space,
in degrees.
Expand Down Expand Up @@ -469,8 +465,35 @@ def read_mesh_boundary(sFilename_boundary_in):


pBoundary_wkt = pBoundary_ogr.ExportToWkt()
pBoundary_shp = loads( pBoundary_ogr.ExportToWkt() )
return pBoundary_wkt, pBoundary_shp

return pBoundary_wkt



def get_geometry_coords(geometry):
if geometry.GetGeometryType() == ogr.wkbPolygon:
return get_polygon_exterior_coords(geometry)
elif geometry.GetGeometryType() == ogr.wkbLineString:
return get_linestring_coords(geometry)
elif geometry.GetGeometryType() == ogr.wkbPoint:
return get_point_coords(geometry)
else:
raise ValueError("Unsupported geometry type.")

def get_polygon_exterior_coords(polygon_geometry):
exterior_coords = []
ring = polygon_geometry.GetGeometryRef(0) # Get the exterior ring
for i in range(ring.GetPointCount()):
point = ring.GetPoint(i)
exterior_coords.append((point[0], point[1]))
return np.array(exterior_coords)

def get_linestring_coords(linestring_geometry):
coords = []
for i in range(linestring_geometry.GetPointCount()):
point = linestring_geometry.GetPoint(i)
coords.append((point[0], point[1]))
return np.array(coords)

def get_point_coords(point_geometry):
point = point_geometry.GetPoint()
return np.array([(point[0], point[1])])
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import os
import json
import numpy as np

import cartopy.crs as ccrs
import cartopy.mpl.ticker as ticker
import matplotlib as mpl
from shapely.wkt import loads
#from shapely.wkt import loads
from osgeo import osr, gdal, ogr


import matplotlib.pyplot as plt
import matplotlib.path as mpath
import matplotlib.ticker as mticker
import matplotlib.patches as mpatches
import matplotlib.cm as cm
from matplotlib import animation
from pyearth.toolbox.data.cgpercentiles import cgpercentiles
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from pyflowline.external.pyearth.gis.gdal.gdal_functions import get_geometry_coords

pProjection = ccrs.PlateCarree() # for latlon data only

Expand Down Expand Up @@ -83,9 +77,10 @@ def animate_vector_polygon_data(
sGeometry_type = pGeometry_in.GetGeometryName()

if sGeometry_type == "POLYGON":
dummy0 = loads(pGeometry_in.ExportToWkt())
aCoords_gcs = dummy0.exterior.coords
aCoords_gcs = np.array(aCoords_gcs)
#dummy0 = loads(pGeometry_in.ExportToWkt())
#aCoords_gcs = dummy0.exterior.coords
#aCoords_gcs = np.array(aCoords_gcs)
aCoords_gcs = get_geometry_coords(pGeometry_in)

dLon_max = np.max([dLon_max, np.max(aCoords_gcs[:, 0])])
dLon_min = np.min([dLon_min, np.min(aCoords_gcs[:, 0])])
Expand Down
Loading

0 comments on commit 488f882

Please sign in to comment.