refine reading Service Area data from inp file
This commit is contained in:
@@ -32,7 +32,7 @@ from .s24_coordinates import inp_in_coord
|
||||
from .s25_vertices import inp_in_vertex
|
||||
from .s26_labels import inp_in_label
|
||||
from .s27_backdrop import inp_in_backdrop
|
||||
from .s32_region import inp_in_region,inp_in_bound
|
||||
from .s32_region import inp_in_region,inp_in_bound,inp_in_regionnodes
|
||||
from .s32_region_util import from_postgis_polygon,to_postgis_polygon
|
||||
|
||||
_S = 'S'
|
||||
@@ -69,6 +69,7 @@ _handler = {
|
||||
VERTICES : (_L, inp_in_vertex),
|
||||
REGION : (_L, inp_in_region),
|
||||
BOUND : (_L, inp_in_bound),
|
||||
REGION_NODES : (_L, inp_in_regionnodes),
|
||||
LABELS : (_L, inp_in_label),
|
||||
BACKDROP : (_S, inp_in_backdrop),
|
||||
#END : 'END',
|
||||
@@ -113,6 +114,7 @@ _level_4 = [
|
||||
VERTICES,
|
||||
REGION,
|
||||
BOUND,
|
||||
REGION_NODES,
|
||||
]
|
||||
|
||||
map_regiontype={
|
||||
@@ -196,6 +198,9 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
||||
current_bound=[]
|
||||
current_bound.clear()
|
||||
region_list={}
|
||||
current_region_nodes=[]
|
||||
current_region_nodes.clear()
|
||||
|
||||
sql_batch = SQLBatch(project)
|
||||
_print_time("Second scan...")
|
||||
with open(inp) as f:
|
||||
@@ -282,12 +287,20 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
||||
vertex_point=(float(tokens[1]),float(tokens[2]))
|
||||
current_bound.append(vertex_point)
|
||||
current_region=tokens[0]
|
||||
|
||||
elif s==REGION_NODES:
|
||||
tokens = line.split()
|
||||
if(tokens[0]!=current_region and len(current_region_nodes)>0):
|
||||
#insert the previous region after get all the vertex of the attatched geometry
|
||||
sql_batch.add(get_insert_into_region_sql(current_region,current_region_nodes))
|
||||
#start the new region
|
||||
current_region_nodes.clear()
|
||||
current_region_nodes.append(tokens[1])
|
||||
current_region=tokens[0]
|
||||
if s == JUNCTIONS:
|
||||
sql_batch.add(handler(line, demand_outside))
|
||||
elif s == PATTERNS:
|
||||
sql_batch.add(handler(line, current_pattern not in variable_patterns))
|
||||
elif s==BOUND:
|
||||
elif s==BOUND or s==REGION_NODES:
|
||||
continue
|
||||
else:
|
||||
sql_batch.add(handler(line))
|
||||
@@ -300,16 +313,38 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
||||
else:
|
||||
sql_batch.add(handler(sections[s]))
|
||||
#need to insert the last region into database
|
||||
if len(current_bound)>0:
|
||||
if len(current_bound)>0:
|
||||
current_bound.append(current_bound[0])
|
||||
current_geometry=to_postgis_polygon(current_bound)
|
||||
region_type=map_regiontype[region_list[current_region]]
|
||||
sql_batch.add(f"insert into region(id, boundary,r_type) values ('{current_region}', '{current_geometry}','{region_type}');")
|
||||
#reset the current region to none for the [REGION_NODES] session reading
|
||||
#current_region=None
|
||||
#need to insert the last region_nodes into database
|
||||
if len(current_region_nodes)>0:
|
||||
sql_batch.add(get_insert_into_region_sql(current_region,current_region_nodes))
|
||||
#current_region=None
|
||||
sql_batch.flush()
|
||||
|
||||
end = _print_time(f'End reading file "{inp}"')
|
||||
print(f"Total (in second): {(end-start).seconds}(s)")
|
||||
|
||||
def get_insert_into_region_sql(region:str,nodes:list[str])->str:
|
||||
str_sql=''
|
||||
str_nodes = str(nodes).replace("'", "''")
|
||||
r_type=region[0:region.index('_')]
|
||||
if r_type == 'DMA' or r_type == 'SA' or r_type == 'VD':
|
||||
table = ''
|
||||
if r_type == 'DMA':
|
||||
table = 'region_dma'
|
||||
elif r_type == 'SA':
|
||||
table = 'region_sa'
|
||||
source=region[region.index('_')+1:]
|
||||
str_sql=f"insert into region_sa(id,time_index,source,nodes) values ('{region}', 0,'{source}','{str_nodes}');"
|
||||
elif r_type == 'VD':
|
||||
table = 'region_vd'
|
||||
|
||||
return str_sql
|
||||
|
||||
def read_inp(project: str, inp: str, version: str = '3') -> bool:
|
||||
if version != '3' and version != '2':
|
||||
|
||||
@@ -87,5 +87,9 @@ def inp_in_region(line: str) -> str:
|
||||
return str(f"insert into _region (id, type) values ('{tokens[0]}', '{tokens[1]}');")
|
||||
|
||||
def inp_in_bound(line: str) -> str:
|
||||
tokens = line.split()
|
||||
return tokens[0]
|
||||
|
||||
def inp_in_regionnodes(line: str)->str:
|
||||
tokens = line.split()
|
||||
return tokens[0]
|
||||
@@ -65,6 +65,7 @@ COORDINATES = 'COORDINATES'
|
||||
VERTICES = 'VERTICES'
|
||||
REGION='REGION'
|
||||
BOUND='BOUND'
|
||||
REGION_NODES='DATA_NODE_OF_REGION'
|
||||
LABELS = 'LABELS'
|
||||
BACKDROP = 'BACKDROP'
|
||||
END = 'END'
|
||||
@@ -74,4 +75,4 @@ section_name = [TITLE, JUNCTIONS, RESERVOIRS, TANKS, PIPES,
|
||||
PATTERNS, CURVES, CONTROLS, RULES, ENERGY,
|
||||
EMITTERS, QUALITY, SOURCES, REACTIONS, MIXING,
|
||||
TIMES, REPORT, OPTIONS, COORDINATES, VERTICES,
|
||||
REGION, BOUND, LABELS, BACKDROP, END]
|
||||
REGION, BOUND, REGION_NODES, LABELS, BACKDROP, END]
|
||||
|
||||
Reference in New Issue
Block a user