Skip to content

Commit 20146a1

Browse files
committed
fix area function bug
1 parent 809d240 commit 20146a1

File tree

7 files changed

+100
-57
lines changed

7 files changed

+100
-57
lines changed

pyflowline/algorithms/auxiliary/gdal_functions.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -278,20 +278,24 @@ def calculate_distance_based_on_lon_lat(lon1, lat1, lon2, lat2):
278278
r = 6378137.0
279279
return c * r
280280

281-
def calculate_polygon_area(lons, lats, algorithm = 0, radius = 6378137.0):
281+
def calculate_polygon_area(aLon_in, aLat_in, algorithm = 0):
282282
"""
283283
Computes area of spherical polygon, assuming spherical Earth.
284284
Returns result in ratio of the sphere's area if the radius is specified. Otherwise, in the units of provided radius.
285285
lats and lons are in degrees.
286286
"""
287287
#TODO: take into account geodesy (i.e. convert latitude to authalic sphere, use radius of authalic sphere instead of mean radius of spherical earth)
288-
lats = np.deg2rad(lats)
289-
lons = np.deg2rad(lons)
290-
288+
#for i in range(len(aLon_in)):
289+
# aLon_in[i] = aLon_in[i] + 180.0
290+
291+
lons = np.deg2rad(aLon_in)
292+
lats = np.deg2rad(aLat_in)
291293
if algorithm==0:
292294
# Line integral based on Green's Theorem, assumes spherical Earth
293295
#close polygon
294-
if lats[0]!=lats[-1]:
296+
if lats[0]==lats[-1] and lons[0]==lons[-1] :
297+
pass
298+
else:
295299
lats = np.append(lats, lats[0])
296300
lons = np.append(lons, lons[0])
297301

@@ -319,10 +323,12 @@ def calculate_polygon_area(lons, lats, algorithm = 0, radius = 6378137.0):
319323
area = abs(sum(integrands))/(4*np.pi) # Could be area of inside or outside
320324

321325
area = min(area,1-area)
322-
if radius is not None: #return in units of radius
323-
return area * 4*np.pi*radius**2
324-
else: #return in ratio of sphere total area
325-
return area
326+
radius= 6378137.0
327+
328+
dArea = area * 4*np.pi*(radius**2)
329+
return dArea
330+
331+
326332
elif algorithm==2:
327333
#L'Huilier Theorem, assumes spherical earth
328334
#see:

pyflowline/algorithms/simplification/remove_returning_flowline.py

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def retrieve_flowline_intersect_index(iSegment_in, iStream_order_in, pVertex_end
6262
if dummy2.is_overlap(dummy3) ==1:
6363
pVertex_end_current = pVertex_start
6464
aCell_flowline.append(lCellID)
65+
#print(lCellID)
6566
iFlag_found = 1
6667
iFlag_previous_overlap =1
6768

@@ -72,6 +73,7 @@ def retrieve_flowline_intersect_index(iSegment_in, iStream_order_in, pVertex_end
7273
iFlag_found = 1
7374
if iFlag_previous_overlap ==1:
7475
aCell_flowline.append(lCellID)
76+
#print(lCellID)
7577
iFlag_previous_overlap=0
7678
pass
7779
else:
@@ -81,6 +83,7 @@ def retrieve_flowline_intersect_index(iSegment_in, iStream_order_in, pVertex_end
8183
else:
8284
pVertex_end_current = pVertex_start
8385
aCell_flowline.append(lCellID)
86+
#print(lCellID)
8487
iFlag_found = 1
8588
pass
8689

pyflowline/classes/basin.py

+51-31
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from pyflowline.formats.read_nhdplus_flowline_shapefile import extract_nhdplus_flowline_shapefile_by_attribute
1414
from pyflowline.formats.read_nhdplus_flowline_shapefile import track_nhdplus_flowline
1515
from pyflowline.formats.convert_flowline_to_geojson import convert_flowline_to_geojson
16-
from pyflowline.formats.export_flowline import export_flowline_to_json
17-
from pyflowline.formats.export_vertex import export_vertex_to_json
16+
from pyflowline.formats.export_flowline import export_flowline_to_geojson
17+
from pyflowline.formats.export_vertex import export_vertex_to_geojson
1818
from pyflowline.algorithms.auxiliary.text_reader_string import text_reader_string
1919
from pyflowline.algorithms.split.find_flowline_vertex import find_flowline_vertex
2020
from pyflowline.algorithms.split.find_flowline_confluence import find_flowline_confluence
@@ -238,15 +238,15 @@ def flowline_simplification(self):
238238
#not used anymore
239239
#aThreshold = np.full(2, 300.0, dtype=float)
240240
#aFlowline_basin_filtered = connect_disconnect_flowline(aFlowline_basin_filtered, aVertex, aThreshold)
241-
#sFilename_out = 'flowline_connect.json'
241+
#sFilename_out = 'flowline_connect.geojson'
242242
#sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
243-
#export_flowline_to_json(iFlag_projected, aFlowline_basin_filtered,pSpatial_reference_gcs, sFilename_out)
243+
#export_flowline_to_geojson(iFlag_projected, aFlowline_basin_filtered,pSpatial_reference_gcs, sFilename_out)
244244
pass
245245
else:
246246
pass
247247

248248
if self.iFlag_debug ==1:
249-
sFilename_out = 'flowline_before_intersect.json'
249+
sFilename_out = 'flowline_before_intersect.geojson'
250250
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
251251
self.export_flowline(aFlowline_basin_filtered, sFilename_out)
252252
#calculate length
@@ -256,14 +256,14 @@ def flowline_simplification(self):
256256
#simplification started
257257
aVertex = find_flowline_vertex(aFlowline_basin_filtered)
258258
if self.iFlag_debug ==1:
259-
sFilename_out = 'flowline_vertex_without_confluence_before_intersect.json'
259+
sFilename_out = 'flowline_vertex_without_confluence_before_intersect.geojson'
260260
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
261-
export_vertex_to_json( aVertex, sFilename_out)
261+
export_vertex_to_geojson( aVertex, sFilename_out)
262262
aFlowline_basin_simplified = split_flowline(aFlowline_basin_filtered, aVertex)
263263
if self.iFlag_debug ==1:
264-
sFilename_out = 'flowline_split_by_point_before_intersect.json'
264+
sFilename_out = 'flowline_split_by_point_before_intersect.geojson'
265265
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
266-
export_flowline_to_json(aFlowline_basin_simplified, sFilename_out)
266+
export_flowline_to_geojson(aFlowline_basin_simplified, sFilename_out)
267267
#ues location to find outlet
268268
point= dict()
269269
point['dLongitude_degree'] = self.dLongitude_outlet_degree
@@ -273,41 +273,41 @@ def flowline_simplification(self):
273273
pVertex_outlet = aFlowline_basin_simplified[0].pVertex_end
274274
self.pVertex_outlet = pVertex_outlet
275275
if self.iFlag_debug ==1:
276-
sFilename_out = 'flowline_direction_before_intersect.json'
276+
sFilename_out = 'flowline_direction_before_intersect.geojson'
277277
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
278-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out)
278+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out)
279279
#step 4: remove loops
280280
aFlowline_basin_simplified = remove_flowline_loop(aFlowline_basin_simplified)
281281
if self.iFlag_debug ==1:
282-
sFilename_out = 'flowline_loop_before_intersect.json'
282+
sFilename_out = 'flowline_loop_before_intersect.geojson'
283283
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
284-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out)
284+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out)
285285
#using loop to remove small river, here we use 5 steps
286286
for i in range(3):
287287
sStep = "{:02d}".format(i+1)
288288
aFlowline_basin_simplified = remove_small_river(aFlowline_basin_simplified, self.dThreshold_small_river)
289289
if self.iFlag_debug ==1:
290-
sFilename_out = 'flowline_large_'+ sStep +'_before_intersect.json'
290+
sFilename_out = 'flowline_large_'+ sStep +'_before_intersect.geojson'
291291
sFilename_out =os.path.join(sWorkspace_output_basin, sFilename_out)
292-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out)
292+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out)
293293
aVertex, lIndex_outlet, aIndex_headwater,aIndex_middle, aIndex_confluence, aConnectivity = find_flowline_confluence(aFlowline_basin_simplified, pVertex_outlet)
294294
if self.iFlag_debug ==1:
295-
sFilename_out = 'flowline_vertex_with_confluence_'+ sStep +'_before_intersect.json'
295+
sFilename_out = 'flowline_vertex_with_confluence_'+ sStep +'_before_intersect.geojson'
296296
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
297-
export_vertex_to_json( aVertex, sFilename_out, aAttribute_data=aConnectivity)
297+
export_vertex_to_geojson( aVertex, sFilename_out, aAttribute_data=aConnectivity)
298298
aFlowline_basin_simplified = merge_flowline( aFlowline_basin_simplified,aVertex, pVertex_outlet, aIndex_headwater,aIndex_middle, aIndex_confluence )
299299
if self.iFlag_debug ==1:
300-
sFilename_out = 'flowline_merge_'+ sStep +'_before_intersect.json'
300+
sFilename_out = 'flowline_merge_'+ sStep +'_before_intersect.geojson'
301301
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
302-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out)
302+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out)
303303
if len(aFlowline_basin_simplified) == 1:
304304
break
305305

306306
#the final vertex info
307307
aVertex, lIndex_outlet, aIndex_headwater,aIndex_middle, aIndex_confluence, aConnectivity = find_flowline_confluence(aFlowline_basin_simplified, pVertex_outlet)
308-
sFilename_out = 'vertex_simplified.json'
308+
sFilename_out = 'vertex_simplified.geojson'
309309
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
310-
export_vertex_to_json( aVertex, sFilename_out, aAttribute_data=aConnectivity)
310+
export_vertex_to_geojson( aVertex, sFilename_out, aAttribute_data=aConnectivity)
311311

312312
#self.dLength_flowline_simplified = self.calculate_flowline_length(aFlowline_basin_simplified)
313313
aVertex = np.array(aVertex)
@@ -322,13 +322,13 @@ def flowline_simplification(self):
322322
if self.iFlag_debug ==1:
323323
sFilename_out = self.sFilename_flowline_segment_index_before_intersect
324324
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
325-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out, \
325+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out, \
326326
aAttribute_data=[aStream_segment], aAttribute_field=['iseg'], aAttribute_dtype=['int'])
327327
#build stream order
328328
aFlowline_basin_simplified, aStream_order = define_stream_order(aFlowline_basin_simplified)
329329
sFilename_out = self.sFilename_flowline_simplified
330330
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
331-
export_flowline_to_json( aFlowline_basin_simplified, sFilename_out, \
331+
export_flowline_to_geojson( aFlowline_basin_simplified, sFilename_out, \
332332
aAttribute_data=[aStream_segment, aStream_order], aAttribute_field=['iseg','iord'], aAttribute_dtype=['int','int'])
333333

334334

@@ -347,6 +347,10 @@ def reconstruct_topological_relationship(self, iMesh_type, sFilename_mesh):
347347
aCell, aCell_intersect_basin, aFlowline_intersect_all = intersect_flowline_with_mesh(iMesh_type, sFilename_mesh, \
348348
sFilename_flowline_in, sFilename_flowline_intersect_out)
349349
sFilename_flowline_filter_geojson = self.sFilename_flowline_filter
350+
if self.iFlag_debug ==1:
351+
sFilename_out = 'flowline_intersect_flowline_with_mesh.geojson'
352+
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
353+
export_flowline_to_geojson(aFlowline_intersect_all, sFilename_out)
350354

351355
point= dict()
352356
point['dLongitude_degree'] = self.dLongitude_outlet_degree
@@ -357,22 +361,38 @@ def reconstruct_topological_relationship(self, iMesh_type, sFilename_mesh):
357361
#segment based
358362
aFlowline_basin_conceptual, lCellID_outlet, pVertex_outlet = remove_returning_flowline(iMesh_type, aCell_intersect_basin, pVertex_outlet_initial)
359363
if self.iFlag_debug ==1:
360-
sFilename_out = 'flowline_simplified_after_intersect.json'
364+
sFilename_out = 'flowline_simplified_after_intersect.geojson'
361365
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
362-
export_flowline_to_json(aFlowline_basin_conceptual, sFilename_out)
366+
export_flowline_to_geojson(aFlowline_basin_conceptual, sFilename_out)
363367

364368
#edge based
365369
aFlowline_basin_conceptual, aEdge = split_flowline_to_edge(aFlowline_basin_conceptual)
370+
if self.iFlag_debug ==1:
371+
sFilename_out = 'flowline_edge_split_flowline_to_edge.geojson'
372+
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
373+
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out)
366374
aFlowline_basin_conceptual = remove_duplicate_flowline(aFlowline_basin_conceptual)
375+
if self.iFlag_debug ==1:
376+
sFilename_out = 'flowline_edge_remove_duplicate_flowline.geojson'
377+
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
378+
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out)
367379
aFlowline_basin_conceptual = correct_flowline_direction(aFlowline_basin_conceptual, pVertex_outlet )
380+
if self.iFlag_debug ==1:
381+
sFilename_out = 'flowline_edge_correct_flowline_direction.geojson'
382+
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
383+
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out)
368384
aFlowline_basin_conceptual = remove_flowline_loop( aFlowline_basin_conceptual )
385+
if self.iFlag_debug ==1:
386+
sFilename_out = 'flowline_edge_remove_flowline_loop.geojson'
387+
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
388+
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out)
369389

370390
aVertex, lIndex_outlet, aIndex_headwater,aIndex_middle, aIndex_confluence, aConnectivity\
371391
= find_flowline_confluence(aFlowline_basin_conceptual, pVertex_outlet)
372392
if self.iFlag_debug ==1:
373-
sFilename_out = 'flowline_vertex_with_confluence_after_intersect.json'
393+
sFilename_out = 'flowline_vertex_with_confluence_after_intersect.geojson'
374394
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
375-
export_vertex_to_json( aVertex, sFilename_out, aAttribute_data=aConnectivity)
395+
export_vertex_to_geojson( aVertex, sFilename_out, aAttribute_data=aConnectivity)
376396

377397
#segment based
378398
aFlowline_basin_conceptual = merge_flowline( aFlowline_basin_conceptual,aVertex, pVertex_outlet, aIndex_headwater,aIndex_middle, aIndex_confluence )
@@ -393,11 +413,11 @@ def reconstruct_topological_relationship(self, iMesh_type, sFilename_mesh):
393413
aFlowline_basin_edge, aEdge = split_flowline_to_edge(aFlowline_basin_conceptual)
394414
sFilename_out = self.sFilename_flowline_edge
395415
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
396-
export_flowline_to_json( aFlowline_basin_edge, sFilename_out)
416+
export_flowline_to_geojson( aFlowline_basin_edge, sFilename_out)
397417

398418
sFilename_out = self.sFilename_flowline_conceptual
399419
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
400-
export_flowline_to_json( aFlowline_basin_conceptual, sFilename_out, \
420+
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out, \
401421
aAttribute_data=[aStream_segment, aStream_order], aAttribute_field=['iseg','iord'], aAttribute_dtype=['int','int'])
402422

403423
self.aFlowline_basin_conceptual = aFlowline_basin_conceptual
@@ -480,7 +500,7 @@ def export(self):
480500
return
481501

482502
def export_flowline(self, aFlowline_in, sFilename_json_in,iFlag_projected_in = None, pSpatial_reference_in = None):
483-
export_flowline_to_json(aFlowline_in, sFilename_json_in,\
503+
export_flowline_to_geojson(aFlowline_in, sFilename_json_in,\
484504
iFlag_projected_in= iFlag_projected_in, \
485505
pSpatial_reference_in = pSpatial_reference_in)
486506

@@ -580,7 +600,7 @@ def export_config_to_json(self, sFilename_output_in = None):
580600
cls=BasinClassEncoder)
581601
return
582602

583-
def convert_flowline_to_json(self):
603+
def convert_flowline_to_geojson(self):
584604
sFilename_raw = self.sFilename_flowline_filter
585605
sFilename_out = self.sFilename_flowline_filter_geojson
586606
print('This is the filtered flowline:', sFilename_raw )

pyflowline/classes/pycase.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def analyze(self):
514514

515515
def setup(self):
516516
if self.iFlag_flowline == 1:
517-
self.convert_flowline_to_json()
517+
self.convert_flowline_to_geojson()
518518
pass
519519
return
520520

pyflowline/formats/convert_flowline_to_geojson.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def convert_shapefile_to_geojson(iFlag_type_in, sFilename_shapefile_in, sFilenam
6868
pSpatial_reference_gcs = osr.SpatialReference()
6969
pSpatial_reference_gcs.ImportFromEPSG(4326)
7070
pSpatial_reference_gcs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
71-
export_flowline_to_json(aFlowline_basin, sFilename_geojson_in)
71+
export_flowline_to_geojson(aFlowline_basin, sFilename_geojson_in)
7272
else:
7373
if iFlag_type_in == 2:
7474
#polygon
@@ -95,7 +95,7 @@ def convert_shapefile_to_geojson_swat(iFlag_type_in, sFilename_shapefile_in, sFi
9595
pSpatial_reference_gcs = osr.SpatialReference()
9696
pSpatial_reference_gcs.ImportFromEPSG(4326)
9797
pSpatial_reference_gcs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
98-
export_flowline_to_json(aFlowline_basin, sFilename_geojson_in)
98+
export_flowline_to_geojson(aFlowline_basin, sFilename_geojson_in)
9999
else:
100100
if iFlag_type_in == 2:
101101
#polygon

pyflowline/formats/export_vertex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from osgeo import ogr, osr
33
from shapely.geometry import Point
44

5-
def export_vertex_to_json(aVertex_in, \
5+
def export_vertex_to_geojson(aVertex_in, \
66
sFilename_json_in,\
77
iFlag_projected_in=None,\
88
pSpatial_reference_in=None, \

0 commit comments

Comments
 (0)