Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ewkt #100

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions geopetl/oracle_sde.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,15 @@ def _prepare_geom(self, geom, srid, transform_srid=None, multi_geom=True):
"""Prepares WKT geometry by projecting and casting as necessary."""
if geom is None or geom == '':
# TODO: should this use the `EMPTY` keyword?
return '{} EMPTY'.format(self.geom_type)
if not self.geom_type:
return 'POINT EMPTY'
else:
return '{} EMPTY'.format(self.geom_type)

# If shape was exported as EWKT (e.g. we exported from postgis with --with_srid), then remove the SRID which is in front of the shape
# which is converting to WKT.
if geom.startswith('SRID='):
geom = geom.split(';')[1]

# Uncomment this to use write method #1 (see write function for details)
# geom = "SDE.ST_Geometry('{}', {})".format(geom, srid)
Expand Down Expand Up @@ -860,7 +868,7 @@ def write(self, rows, srid=None, table_srid=None,
fields = sorted(fields, key=lambda x: 'lob' in self.metadata[x]['type'])

table_geom_field = self.geom_field
srid = srid or self.srid
srid = srid or self.srid
table_geom_type = self.geom_type if table_geom_field else None
# row_geom_type = re.match('[A-Z]+', rows[0][geom_field]).group() \
# if geom_field else None
Expand All @@ -873,6 +881,8 @@ def write(self, rows, srid=None, table_srid=None,
rows_geom_field = None
for i, val in enumerate(first_row):
# TODO make a function to screen for wkt-like text
if str(val).startswith('SRID='):
val = val.split(';')[1]
if str(val).startswith(('POINT', 'POLYGON', 'LINESTRING', 'MULTIPOLYGON')):
if rows_geom_field:
raise ValueError('Multiple geometry fields found: {}'.format(', '.join([rows_geom_field, first_row_header[i]])))
Expand All @@ -885,6 +895,10 @@ def write(self, rows, srid=None, table_srid=None,
geom_rows = rows.selectnotnone(rows_geom_field).records()
geom_row = geom_rows[0]
geom = geom_row[rows_geom_field]
# Override SRID we'll use to insert with if we find the SRID in the shape field in the CSV.
# This is so we can insert shapes into unregistered tables.
if geom.startswith('SRID='):
srid = geom.split(';')[0].split('=')[1]
# if geom value is empty in staging data, insert "point table"
if not geom:
geom = 'POINT EMPTY'
Expand Down
Loading