Skip to content

Commit

Permalink
clean data with z geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoll1 committed Mar 23, 2024
1 parent 91cb89d commit 0b30b4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions osmand_osm/osm/oa.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def merge_oa(self, db_name):
logging.info(source.table + ' Insert ' + str(cur.rowcount))
except psycopg.errors.InvalidParameterValue as error:
logging.error(f'{source.table} errored on merge_oa insert: {error}')
conn.rollback()
# get rid of z geometry
cur.execute(f'alter table {source.table_temp} alter column wkb_geometry type geometry(point, 4326) using ST_Force2D(wkb_geometry);')
cur.execute(f'insert into {source.table} select * from {source.table_temp} on conflict do nothing')
# get rid of temp table
cur.execute(f'drop table {source.table_temp}')
conn.commit()
Expand Down
47 changes: 46 additions & 1 deletion osmand_osm/osm/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ def test_filter_data(self):
data = self.cur.fetchall()
self.assertEqual(len(data), 1)


def test_merge_oa(self):
'''
test additional data inserted from temp table when table already exists with data
Expand Down Expand Up @@ -252,6 +251,52 @@ def test_merge_oa_first_run(self):
self.assertIn((2, None, '115', 'NW 41st ST', None, None, None, None, '98107', '87d28792bee6b164', '0101000020E6100000DD7C23BAE7965EC0FC3ACB87FBD34740'), data)
self.assertIn((1, None, '119', 'NW 41st ST', None, None, None, None, '98107', 'e8605a496593386e', '0101000020E61000003BEFB556EA965EC03EFC4685FBD34740'), data)

def test_merge_oa_zdata(self):
'''
test that addresses with z data cleaned and inserted into db
'''
# cleanup postgres table
self.cur.execute("drop table if exists aa_merge_oa_addresses_city;")
self.cur.execute("drop table if exists aa_merge_oa_addresses_city_temp;")
self.conn.commit()
# load data into postgres
self.cur.execute("create table aa_merge_oa_addresses_city_temp (ogc_fid integer NOT NULL, \
id character varying, number character varying, street character varying, unit character varying, \
city character varying, district character varying, region character varying, \
postcode character varying, hash character varying, wkb_geometry public.geometry(PointZ, 4326));")
self.cur.execute("insert into aa_merge_oa_addresses_city_temp (ogc_fid, number, street, postcode, hash, wkb_geometry) \
values (%s, %s, %s, %s, %s, ST_GEOMFromText(%s, 4326))" \
, (1, '119', 'NW 41st ST', '98107', 'e8605a496593386e', 'POINT(-122.3580529 47.6561133 0.0)'))
self.cur.execute("insert into aa_merge_oa_addresses_city_temp (ogc_fid, number, street, postcode, hash, wkb_geometry) \
values (%s, %s, %s, %s, %s, ST_GEOMFromText(%s, 4326))" \
, (2, '115', 'NW 41st ST', '98107', '87d28792bee6b164', 'POINT(-122.3578935 47.6561136 0.0)'))
self.cur.execute("insert into aa_merge_oa_addresses_city_temp (ogc_fid, number, street, postcode, hash, wkb_geometry) \
values (%s, %s, %s, %s, %s, ST_GEOMFromText(%s, 4326))" \
, (3, '34', 'BARR ROAD', '98643', '2a7175361ea6f935', 'POINT(-123.6361676 46.32554 0.0)'))
# create table
self.cur.execute("create table aa_merge_oa_addresses_city (ogc_fid integer NOT NULL, \
id character varying, number character varying, street character varying, \
unit character varying, city character varying, district character varying, \
region character varying, postcode character varying, hash character varying, \
wkb_geometry public.geometry(Point, 4326));")
self.cur.execute('alter table aa_merge_oa_addresses_city add constraint aa_merge_oa_addresses_city_unique \
unique nulls not distinct (number, street, unit, wkb_geometry)')
self.conn.commit()
working_area = oa.WorkingArea('aa')
working_area.master_list = [oa.Source(Path('aa/merge-oa-addresses-city.geojson'))]
working_area.merge_oa(DB_NAME)
self.cur.execute("select * from aa_merge_oa_addresses_city;")
data = self.cur.fetchall()
# check that good data exists
self.assertIn((2, None, '115', 'NW 41st ST', None, None, None, None, '98107', \
'87d28792bee6b164', '0101000020E6100000DD7C23BAE7965EC0FC3ACB87FBD34740'), data)
self.assertIn((1, None, '119', 'NW 41st ST', None, None, None, None, '98107', \
'e8605a496593386e', '0101000020E61000003BEFB556EA965EC03EFC4685FBD34740'), data)
# check if z still in geom data
self.cur.execute("select ST_Z(wkb_geometry) from aa_merge_oa_addresses_city where number='34';")
data = self.cur.fetchall()
self.assertEqual(data[0][0], None)

def test_output_osm_ids(self):
'''
make sure address ids in output files don't overlap
Expand Down

0 comments on commit 0b30b4b

Please sign in to comment.