修复引用错误
This commit is contained in:
+96
-65
@@ -1,6 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
from .project_backup import *
|
from .project import *
|
||||||
from .database import ChangeSet, write
|
from .database import ChangeSet, write
|
||||||
from .sections import *
|
from .sections import *
|
||||||
from .s0_base import get_region_type
|
from .s0_base import get_region_type
|
||||||
@@ -38,11 +38,13 @@ from .s32_region_util import from_postgis_polygon,to_postgis_polygon
|
|||||||
# DingZQ, 2024-12-28, export inp
|
# DingZQ, 2024-12-28, export inp
|
||||||
from .inp_out import export_inp
|
from .inp_out import export_inp
|
||||||
|
|
||||||
_S = 'S'
|
_S = "S"
|
||||||
_L = 'L'
|
_L = "L"
|
||||||
|
|
||||||
|
|
||||||
|
def _inp_in_option(section: list[str], version: str = "3") -> str:
|
||||||
|
return inp_in_option_v3(section) if version == "3" else inp_in_option(section)
|
||||||
|
|
||||||
def _inp_in_option(section: list[str], version: str = '3') -> str:
|
|
||||||
return inp_in_option_v3(section) if version == '3' else inp_in_option(section)
|
|
||||||
|
|
||||||
_handler = {
|
_handler = {
|
||||||
TITLE: (_S, inp_in_title),
|
TITLE: (_S, inp_in_title),
|
||||||
@@ -122,12 +124,14 @@ _level_4 = [
|
|||||||
|
|
||||||
map_regiontype = {
|
map_regiontype = {
|
||||||
# map the region types from desktop to server
|
# map the region types from desktop to server
|
||||||
'DISTRIBUTION':'WDA',
|
"DISTRIBUTION": "WDA",
|
||||||
'DMA':'DMA',
|
"DMA": "DMA",
|
||||||
'PMA':'PMA',
|
"PMA": "PMA",
|
||||||
'VD':'VD',
|
"VD": "VD",
|
||||||
'SA':'SA',
|
"SA": "SA",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class SQLBatch:
|
class SQLBatch:
|
||||||
def __init__(self, project: str, count: int = 100) -> None:
|
def __init__(self, project: str, count: int = 100) -> None:
|
||||||
self.batch: list[str] = []
|
self.batch: list[str] = []
|
||||||
@@ -140,13 +144,13 @@ class SQLBatch:
|
|||||||
self.flush()
|
self.flush()
|
||||||
|
|
||||||
def flush(self) -> None:
|
def flush(self) -> None:
|
||||||
write(self.project, ''.join(self.batch))
|
write(self.project, "".join(self.batch))
|
||||||
self.batch.clear()
|
self.batch.clear()
|
||||||
|
|
||||||
|
|
||||||
def _print_time(desc: str) -> datetime.datetime:
|
def _print_time(desc: str) -> datetime.datetime:
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
time = now.strftime('%Y-%m-%d %H:%M:%S')
|
time = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
print(f"{time}: {desc}")
|
print(f"{time}: {desc}")
|
||||||
return now
|
return now
|
||||||
|
|
||||||
@@ -154,7 +158,7 @@ def _print_time(desc: str) -> datetime.datetime:
|
|||||||
def _get_file_offset(inp: str) -> tuple[dict[str, list[int]], bool]:
|
def _get_file_offset(inp: str) -> tuple[dict[str, list[int]], bool]:
|
||||||
offset: dict[str, list[int]] = {}
|
offset: dict[str, list[int]] = {}
|
||||||
|
|
||||||
current = ''
|
current = ""
|
||||||
demand_outside = False
|
demand_outside = False
|
||||||
|
|
||||||
with open(inp) as f:
|
with open(inp) as f:
|
||||||
@@ -164,22 +168,22 @@ def _get_file_offset(inp: str) -> tuple[dict[str, list[int]], bool]:
|
|||||||
break
|
break
|
||||||
|
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line.startswith('['):
|
if line.startswith("["):
|
||||||
for s in section_name:
|
for s in section_name:
|
||||||
if line.startswith(f'[{s}'):
|
if line.startswith(f"[{s}"):
|
||||||
if s not in offset:
|
if s not in offset:
|
||||||
offset[s] = []
|
offset[s] = []
|
||||||
offset[s].append(f.tell())
|
offset[s].append(f.tell())
|
||||||
current = s
|
current = s
|
||||||
break
|
break
|
||||||
elif line != '' and line.startswith(';') == False:
|
elif line != "" and line.startswith(";") == False:
|
||||||
if current == DEMANDS:
|
if current == DEMANDS:
|
||||||
demand_outside = True
|
demand_outside = True
|
||||||
|
|
||||||
return (offset, demand_outside)
|
return (offset, demand_outside)
|
||||||
|
|
||||||
|
|
||||||
def parse_file(project: str, inp: str, version: str = '3') -> None:
|
def parse_file(project: str, inp: str, version: str = "3") -> None:
|
||||||
start = _print_time(f'Start reading file "{inp}"...')
|
start = _print_time(f'Start reading file "{inp}"...')
|
||||||
|
|
||||||
_print_time("First scan...")
|
_print_time("First scan...")
|
||||||
@@ -228,17 +232,17 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
break
|
break
|
||||||
|
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line.startswith('['):
|
if line.startswith("["):
|
||||||
break
|
break
|
||||||
elif line == '':
|
elif line == "":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if is_s:
|
if is_s:
|
||||||
sections[s].append(line)
|
sections[s].append(line)
|
||||||
else:
|
else:
|
||||||
if line.startswith(';'):
|
if line.startswith(";"):
|
||||||
if version != '3': #v2
|
if version != "3": # v2
|
||||||
line = line.removeprefix(';')
|
line = line.removeprefix(";")
|
||||||
if s == PATTERNS: # ;desc
|
if s == PATTERNS: # ;desc
|
||||||
pass
|
pass
|
||||||
elif s == CURVES: # ;type: desc
|
elif s == CURVES: # ;type: desc
|
||||||
@@ -249,29 +253,37 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
if tokens[1].upper() in pattern_v3_types: # v3
|
if tokens[1].upper() in pattern_v3_types: # v3
|
||||||
sql_batch.add(f"insert into _pattern (id) values ('{tokens[0]}');")
|
sql_batch.add(
|
||||||
|
f"insert into _pattern (id) values ('{tokens[0]}');"
|
||||||
|
)
|
||||||
current_pattern = tokens[0]
|
current_pattern = tokens[0]
|
||||||
if tokens[1].upper() == 'VARIABLE':
|
if tokens[1].upper() == "VARIABLE":
|
||||||
variable_patterns.append(tokens[0])
|
variable_patterns.append(tokens[0])
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if current_pattern != tokens[0]:
|
if current_pattern != tokens[0]:
|
||||||
sql_batch.add(f"insert into _pattern (id) values ('{tokens[0]}');")
|
sql_batch.add(
|
||||||
|
f"insert into _pattern (id) values ('{tokens[0]}');"
|
||||||
|
)
|
||||||
current_pattern = tokens[0]
|
current_pattern = tokens[0]
|
||||||
|
|
||||||
elif s == CURVES:
|
elif s == CURVES:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
|
|
||||||
if tokens[1].upper() in curve_types: # v3
|
if tokens[1].upper() in curve_types: # v3
|
||||||
sql_batch.add(f"insert into _curve (id, type) values ('{tokens[0]}', '{tokens[1].upper()}');")
|
sql_batch.add(
|
||||||
|
f"insert into _curve (id, type) values ('{tokens[0]}', '{tokens[1].upper()}');"
|
||||||
|
)
|
||||||
current_curve = tokens[0]
|
current_curve = tokens[0]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if current_curve != tokens[0]:
|
if current_curve != tokens[0]:
|
||||||
type = curve_types[0]
|
type = curve_types[0]
|
||||||
if curve_type_desc_line != None:
|
if curve_type_desc_line != None:
|
||||||
type = curve_type_desc_line.split(':')[0].strip()
|
type = curve_type_desc_line.split(":")[0].strip()
|
||||||
sql_batch.add(f"insert into _curve (id, type) values ('{tokens[0]}', '{type}');")
|
sql_batch.add(
|
||||||
|
f"insert into _curve (id, type) values ('{tokens[0]}', '{type}');"
|
||||||
|
)
|
||||||
current_curve = tokens[0]
|
current_curve = tokens[0]
|
||||||
curve_type_desc_line = None
|
curve_type_desc_line = None
|
||||||
elif s == REGION:
|
elif s == REGION:
|
||||||
@@ -279,12 +291,14 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
region_list[tokens[0]] = tokens[1]
|
region_list[tokens[0]] = tokens[1]
|
||||||
elif s == BOUND:
|
elif s == BOUND:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
if(tokens[0]!=current_region and len(current_bound)>0):
|
if tokens[0] != current_region and len(current_bound) > 0:
|
||||||
# insert the previous region after get all the vertex of the attatched geometry
|
# insert the previous region after get all the vertex of the attatched geometry
|
||||||
current_bound.append(current_bound[0])
|
current_bound.append(current_bound[0])
|
||||||
current_geometry = to_postgis_polygon(current_bound)
|
current_geometry = to_postgis_polygon(current_bound)
|
||||||
region_type = map_regiontype[region_list[tokens[0]]]
|
region_type = map_regiontype[region_list[tokens[0]]]
|
||||||
sql_batch.add(f"insert into region(id, boundary,r_type) values ('{current_region}', '{current_geometry}','{region_type}');")
|
sql_batch.add(
|
||||||
|
f"insert into region(id, boundary,r_type) values ('{current_region}', '{current_geometry}','{region_type}');"
|
||||||
|
)
|
||||||
# start the new region
|
# start the new region
|
||||||
current_bound.clear()
|
current_bound.clear()
|
||||||
vertex_point = (float(tokens[1]), float(tokens[2]))
|
vertex_point = (float(tokens[1]), float(tokens[2]))
|
||||||
@@ -292,9 +306,16 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
current_region = tokens[0]
|
current_region = tokens[0]
|
||||||
elif s == REGION_NODES:
|
elif s == REGION_NODES:
|
||||||
tokens = line.split()
|
tokens = line.split()
|
||||||
if(tokens[0]!=current_region and len(current_region_nodes)>0):
|
if (
|
||||||
|
tokens[0] != current_region
|
||||||
|
and len(current_region_nodes) > 0
|
||||||
|
):
|
||||||
# insert the previous region after get all the vertex of the attatched geometry
|
# 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))
|
sql_batch.add(
|
||||||
|
get_insert_into_region_sql(
|
||||||
|
current_region, current_region_nodes
|
||||||
|
)
|
||||||
|
)
|
||||||
# start the new region
|
# start the new region
|
||||||
current_region_nodes.clear()
|
current_region_nodes.clear()
|
||||||
current_region_nodes.append(tokens[1])
|
current_region_nodes.append(tokens[1])
|
||||||
@@ -302,7 +323,9 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
if s == JUNCTIONS:
|
if s == JUNCTIONS:
|
||||||
sql_batch.add(handler(line, demand_outside))
|
sql_batch.add(handler(line, demand_outside))
|
||||||
elif s == PATTERNS:
|
elif s == PATTERNS:
|
||||||
sql_batch.add(handler(line, current_pattern not in variable_patterns))
|
sql_batch.add(
|
||||||
|
handler(line, current_pattern not in variable_patterns)
|
||||||
|
)
|
||||||
elif s == BOUND or s == REGION_NODES:
|
elif s == BOUND or s == REGION_NODES:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
@@ -320,38 +343,44 @@ def parse_file(project: str, inp: str, version: str = '3') -> None:
|
|||||||
current_bound.append(current_bound[0])
|
current_bound.append(current_bound[0])
|
||||||
current_geometry = to_postgis_polygon(current_bound)
|
current_geometry = to_postgis_polygon(current_bound)
|
||||||
region_type = map_regiontype[region_list[current_region]]
|
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}');")
|
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
|
# reset the current region to none for the [REGION_NODES] session reading
|
||||||
# current_region=None
|
# current_region=None
|
||||||
# need to insert the last region_nodes into database
|
# need to insert the last region_nodes into database
|
||||||
if len(current_region_nodes) > 0:
|
if len(current_region_nodes) > 0:
|
||||||
sql_batch.add(get_insert_into_region_sql(current_region,current_region_nodes))
|
sql_batch.add(
|
||||||
|
get_insert_into_region_sql(current_region, current_region_nodes)
|
||||||
|
)
|
||||||
# current_region=None
|
# current_region=None
|
||||||
sql_batch.flush()
|
sql_batch.flush()
|
||||||
|
|
||||||
end = _print_time(f'End reading file "{inp}"')
|
end = _print_time(f'End reading file "{inp}"')
|
||||||
print(f"Total (in second): {(end-start).seconds}(s)")
|
print(f"Total (in second): {(end-start).seconds}(s)")
|
||||||
|
|
||||||
|
|
||||||
def get_insert_into_region_sql(region: str, nodes: list[str]) -> str:
|
def get_insert_into_region_sql(region: str, nodes: list[str]) -> str:
|
||||||
str_sql=''
|
str_sql = ""
|
||||||
str_nodes = str(nodes).replace("'", "''")
|
str_nodes = str(nodes).replace("'", "''")
|
||||||
r_type=region[0:region.index('_')]
|
r_type = region[0 : region.index("_")]
|
||||||
if r_type == 'DMA' or r_type == 'SA' or r_type == 'VD':
|
if r_type == "DMA" or r_type == "SA" or r_type == "VD":
|
||||||
table = ''
|
table = ""
|
||||||
if r_type == 'DMA':
|
if r_type == "DMA":
|
||||||
table = 'region_dma'
|
table = "region_dma"
|
||||||
elif r_type == 'SA':
|
elif r_type == "SA":
|
||||||
table = 'region_sa'
|
table = "region_sa"
|
||||||
source=region[region.index('_')+1:]
|
source = region[region.index("_") + 1 :]
|
||||||
str_sql = f"insert into region_sa(id,time_index,source,nodes) values ('{region}', 0,'{source}','{str_nodes}');"
|
str_sql = f"insert into region_sa(id,time_index,source,nodes) values ('{region}', 0,'{source}','{str_nodes}');"
|
||||||
elif r_type == 'VD':
|
elif r_type == "VD":
|
||||||
table = 'region_vd'
|
table = "region_vd"
|
||||||
|
|
||||||
return str_sql
|
return str_sql
|
||||||
|
|
||||||
def read_inp(project: str, inp: str, version: str = '3') -> bool:
|
|
||||||
if version != '3' and version != '2':
|
def read_inp(project: str, inp: str, version: str = "3") -> bool:
|
||||||
version = '2'
|
if version != "3" and version != "2":
|
||||||
|
version = "2"
|
||||||
|
|
||||||
if is_project_open(project):
|
if is_project_open(project):
|
||||||
close_project(project)
|
close_project(project)
|
||||||
@@ -364,19 +393,20 @@ def read_inp(project: str, inp: str, version: str = '3') -> bool:
|
|||||||
|
|
||||||
parse_file(project, inp, version)
|
parse_file(project, inp, version)
|
||||||
|
|
||||||
'''try:
|
"""try:
|
||||||
parse_file(project, inp, version)
|
parse_file(project, inp, version)
|
||||||
except:
|
except:
|
||||||
close_project(project)
|
close_project(project)
|
||||||
delete_project(project)
|
delete_project(project)
|
||||||
return False'''
|
return False"""
|
||||||
|
|
||||||
close_project(project)
|
close_project(project)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# DingZQ, 2024-12-28, convert v3 to v2
|
# DingZQ, 2024-12-28, convert v3 to v2
|
||||||
def convert_inp_v3_to_v2(inp: str) -> ChangeSet:
|
def convert_inp_v3_to_v2(inp: str) -> ChangeSet:
|
||||||
project = 'v3Tov2'
|
project = "v3Tov2"
|
||||||
|
|
||||||
if is_project_open(project):
|
if is_project_open(project):
|
||||||
close_project(project)
|
close_project(project)
|
||||||
@@ -387,38 +417,39 @@ def convert_inp_v3_to_v2(inp: str) -> ChangeSet:
|
|||||||
create_project(project)
|
create_project(project)
|
||||||
open_project(project)
|
open_project(project)
|
||||||
|
|
||||||
filename = f'inp/{project}_temp.inp'
|
filename = f"inp/{project}_temp.inp"
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
with open(filename, 'w') as f:
|
with open(filename, "w") as f:
|
||||||
f.write(inp)
|
f.write(inp)
|
||||||
|
|
||||||
parse_file(project, filename, '3')
|
parse_file(project, filename, "3")
|
||||||
|
|
||||||
'''try:
|
"""try:
|
||||||
parse_file(project, inp, version)
|
parse_file(project, inp, version)
|
||||||
except:
|
except:
|
||||||
close_project(project)
|
close_project(project)
|
||||||
delete_project(project)
|
delete_project(project)
|
||||||
return False'''
|
return False"""
|
||||||
|
|
||||||
return export_inp(project, '2')
|
return export_inp(project, "2")
|
||||||
|
|
||||||
def import_inp(project: str, cs: ChangeSet, version: str = '3') -> bool:
|
|
||||||
if version != '3' and version != '2':
|
|
||||||
version = '2'
|
|
||||||
|
|
||||||
if 'inp' not in cs.operations[0]:
|
def import_inp(project: str, cs: ChangeSet, version: str = "3") -> bool:
|
||||||
|
if version != "3" and version != "2":
|
||||||
|
version = "2"
|
||||||
|
|
||||||
|
if "inp" not in cs.operations[0]:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
filename = f'inp/{project}_temp.inp'
|
filename = f"inp/{project}_temp.inp"
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
_print_time(f'Start writing temp file "{filename}"...')
|
_print_time(f'Start writing temp file "{filename}"...')
|
||||||
with open(filename, 'w',encoding="GBK") as f:
|
with open(filename, "w", encoding="GBK") as f:
|
||||||
f.write(str(cs.operations[0]['inp']))
|
f.write(str(cs.operations[0]["inp"]))
|
||||||
_print_time(f'End writing temp file "{filename}"...')
|
_print_time(f'End writing temp file "{filename}"...')
|
||||||
|
|
||||||
result = read_inp(project, filename, version)
|
result = read_inp(project, filename, version)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from .project_backup import *
|
from .project import *
|
||||||
from .database import ChangeSet
|
from .database import ChangeSet
|
||||||
from .sections import *
|
from .sections import *
|
||||||
from .s1_title import inp_out_title
|
from .s1_title import inp_out_title
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import ctypes
|
import ctypes
|
||||||
from .project_backup import have_project
|
from .project import have_project
|
||||||
from .inp_out import dump_inp
|
from .inp_out import dump_inp
|
||||||
|
|
||||||
def calculate_service_area(name: str) -> list[dict[str, list[str]]]:
|
def calculate_service_area(name: str) -> list[dict[str, list[str]]]:
|
||||||
|
|||||||
Reference in New Issue
Block a user