Skip to content

Commit

Permalink
Encapsulate creating ogr2ogr arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git committed Nov 15, 2022
1 parent 946c842 commit 630583b
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions src/layman/layer/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,9 @@ def import_layer_vector_file(workspace, layername, main_filepath, crs_id):
raise LaymanError(11, private_data=pg_error)


def import_layer_vector_file_async_with_iconv(schema, table_name, main_filepath, crs_id):
import subprocess
assert table_name, f'schema={schema}, table_name={table_name}, main_filepath={main_filepath}'
def create_ogr2ogr_args(*, schema, table_name, main_filepath, crs_id, output):
pg_conn = ' '.join([f"{k}='{v}'" for k, v in PG_CONN.items()])

first_ogr2ogr_args = [
'ogr2ogr',
'--config', 'OGR_ENABLE_PARTIAL_REPROJECTION', 'TRUE',
'-unsetFid',
'-a_srs', crs_id,
'-f', 'GeoJSON',
'/vsistdout/',
f'{main_filepath}',
]
iconv_args = [
'iconv',
'-c',
'-t', 'utf8',
]
final_ogr2ogr_args = [
ogr2ogr_args = [
'ogr2ogr',
'-nln', table_name,
'-nlt', 'GEOMETRY',
Expand All @@ -135,16 +118,42 @@ def import_layer_vector_file_async_with_iconv(schema, table_name, main_filepath,
# 'PG:{} active_schema={}'.format(PG_CONN, username),
]
if crs_id is not None:
final_ogr2ogr_args.extend([
ogr2ogr_args.extend([
'-a_srs', crs_id,
])
if os.path.splitext(main_filepath)[1] == '.shp':
final_ogr2ogr_args.extend([
ogr2ogr_args.extend([
'-lco', 'PRECISION=NO',
])
final_ogr2ogr_args.extend([
'/vsistdin/',
ogr2ogr_args.extend([
output,
])
return ogr2ogr_args


def import_layer_vector_file_async_with_iconv(schema, table_name, main_filepath, crs_id):
import subprocess
assert table_name, f'schema={schema}, table_name={table_name}, main_filepath={main_filepath}'

first_ogr2ogr_args = [
'ogr2ogr',
'--config', 'OGR_ENABLE_PARTIAL_REPROJECTION', 'TRUE',
'-unsetFid',
'-a_srs', crs_id,
'-f', 'GeoJSON',
'/vsistdout/',
f'{main_filepath}',
]
iconv_args = [
'iconv',
'-c',
'-t', 'utf8',
]
final_ogr2ogr_args = create_ogr2ogr_args(schema=schema,
table_name=table_name,
main_filepath=main_filepath,
crs_id=crs_id,
output='/vsistdin/')

first_ogr2ogr_process = subprocess.Popen(first_ogr2ogr_args,
stdout=subprocess.PIPE)
Expand All @@ -164,30 +173,11 @@ def import_layer_vector_file_async(schema, table_name, main_filepath,
# import file to database table
import subprocess
assert table_name, f'schema={schema}, table_name={table_name}, main_filepath={main_filepath}'
pg_conn = ' '.join([f"{k}='{v}'" for k, v in PG_CONN.items()])
bash_args = [
'ogr2ogr',
'-nln', table_name,
'-nlt', 'GEOMETRY',
'--config', 'OGR_ENABLE_PARTIAL_REPROJECTION', 'TRUE',
'-lco', f'SCHEMA={schema}',
# '-clipsrc', '-180', '-85.06', '180', '85.06',
'-f', 'PostgreSQL',
'-unsetFid',
f'PG:{pg_conn}',
# 'PG:{} active_schema={}'.format(PG_CONN, username),
]
if crs_id is not None:
bash_args.extend([
'-a_srs', crs_id,
])
if os.path.splitext(main_filepath)[1] == '.shp':
bash_args.extend([
'-lco', 'PRECISION=NO',
])
bash_args.extend([
f'{main_filepath}',
])
bash_args = create_ogr2ogr_args(schema=schema,
table_name=table_name,
main_filepath=main_filepath,
crs_id=crs_id,
output=main_filepath)

# print(' '.join(bash_args))
process = subprocess.Popen(bash_args, stdout=subprocess.PIPE,
Expand Down

0 comments on commit 630583b

Please sign in to comment.