Skip to content

Commit

Permalink
fixed: record-named-area-map add via
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed Mar 5, 2024
1 parent e3b3754 commit 10b5499
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 37 deletions.
29 changes: 29 additions & 0 deletions alembic/versions/b34921972c96_record_na_assiciate2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""record-na-assiciate2
Revision ID: b34921972c96
Revises: edabe202477f
Create Date: 2024-03-04 20:30:14.929427
"""
from alembic import op
import sqlalchemy as sa

import geoalchemy2

# revision identifiers, used by Alembic.
revision = 'b34921972c96'
down_revision = 'edabe202477f'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
128 changes: 128 additions & 0 deletions alembic/versions/e1fda716c3ec_record_named_area_map_via.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""record-named-area-map-via
Revision ID: e1fda716c3ec
Revises: 5b41d404cb3b
Create Date: 2024-03-04 18:29:22.760739
"""
from alembic import op
import sqlalchemy as sa
from geoalchemy2 import Geometry
import geoalchemy2

# revision identifiers, used by Alembic.
revision = 'e1fda716c3ec'
down_revision = '5b41d404cb3b'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_geospatial_index('protect_area_geom_idx', table_name='protect_area', postgresql_using='gist', column_name='geom')
op.drop_geospatial_table('protect_area')
op.drop_geospatial_table('town_majia_sanhe')
op.drop_geospatial_table('wild_1043')
op.drop_geospatial_index('village_nlsc_1120317_geom_idx', table_name='village_nlsc_1120317', postgresql_using='gist', column_name='geom')
op.drop_geospatial_table('village_nlsc_1120317')
op.drop_geospatial_table('town_moi_1120825')
op.drop_geospatial_index('county_moi_1090820_geom_idx', table_name='county_moi_1090820', postgresql_using='gist', column_name='geom')
op.drop_geospatial_table('county_moi_1090820')
op.drop_geospatial_table('world_administrative_boundaries')
op.add_column('record_named_area_map', sa.Column('via', sa.String(length=10), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('record_named_area_map', 'via')
op.create_geospatial_table('world_administrative_boundaries',
sa.Column('gid', sa.INTEGER(), server_default=sa.text('nextval(\'"world-administrative-boundaries_gid_seq"\'::regclass)'), autoincrement=True, nullable=False),
sa.Column('iso3', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('status', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('color_code', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('name', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('continent', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('region', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('iso_3166_1_', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('french_shor', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='world-administrative-boundaries_pkey')
)
op.create_geospatial_table('county_moi_1090820',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('countyid', sa.VARCHAR(length=3), autoincrement=False, nullable=True),
sa.Column('countycode', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('countyname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('countyeng', sa.VARCHAR(length=39), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='county_moi_1090820_pkey')
)
op.create_geospatial_index('county_moi_1090820_geom_idx', 'county_moi_1090820', ['geom'], unique=False, postgresql_using='gist', postgresql_ops={})
op.create_geospatial_table('town_moi_1120825',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('townid', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('towncode', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('countyname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('townname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('towneng', sa.VARCHAR(length=39), autoincrement=False, nullable=True),
sa.Column('countyid', sa.VARCHAR(length=3), autoincrement=False, nullable=True),
sa.Column('countycode', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='town_moi_1120825_pkey')
)
op.create_geospatial_table('village_nlsc_1120317',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('villcode', sa.VARCHAR(length=18), autoincrement=False, nullable=True),
sa.Column('countyname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('townname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('villname', sa.VARCHAR(length=39), autoincrement=False, nullable=True),
sa.Column('villeng', sa.VARCHAR(length=39), autoincrement=False, nullable=True),
sa.Column('countyid', sa.VARCHAR(length=3), autoincrement=False, nullable=True),
sa.Column('countycode', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('townid', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('towncode', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('note', sa.VARCHAR(length=30), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='village_nlsc_1120317_pkey')
)
op.create_geospatial_index('village_nlsc_1120317_geom_idx', 'village_nlsc_1120317', ['geom'], unique=False, postgresql_using='gist', postgresql_ops={})
op.create_geospatial_table('wild_1043',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('name', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.Column('c_name', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.Column('id', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('file_name', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
sa.Column('shape_leng', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('shape_area', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='wild_1043_pkey')
)
op.create_geospatial_table('town_majia_sanhe',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('townid', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('towncode', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('countyname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('townname', sa.VARCHAR(length=12), autoincrement=False, nullable=True),
sa.Column('towneng', sa.VARCHAR(length=39), autoincrement=False, nullable=True),
sa.Column('countyid', sa.VARCHAR(length=3), autoincrement=False, nullable=True),
sa.Column('countycode', sa.VARCHAR(length=8), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='town_majia_sanhe_pkey')
)
op.create_geospatial_table('protect_area',
sa.Column('gid', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('file_name', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
sa.Column('name', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
sa.Column('shape_leng', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('date', sa.DATE(), autoincrement=False, nullable=True),
sa.Column('announce', sa.DATE(), autoincrement=False, nullable=True),
sa.Column('area_ha', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('id', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('shape_star', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('shape_stle', sa.NUMERIC(), autoincrement=False, nullable=True),
sa.Column('geom', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry', _spatial_index_reflected=True), autoincrement=False, nullable=True),
sa.PrimaryKeyConstraint('gid', name='protect_area_pkey')
)
op.create_geospatial_index('protect_area_geom_idx', 'protect_area', ['geom'], unique=False, postgresql_using='gist', postgresql_ops={})
# ### end Alembic commands ###
29 changes: 29 additions & 0 deletions alembic/versions/edabe202477f_record_na_assiciate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""record-na-assiciate
Revision ID: edabe202477f
Revises: e1fda716c3ec
Create Date: 2024-03-04 19:50:52.439762
"""
from alembic import op
import sqlalchemy as sa

import geoalchemy2

# revision identifiers, used by Alembic.
revision = 'edabe202477f'
down_revision = 'e1fda716c3ec'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
3 changes: 1 addition & 2 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def record_list():
#fav_list = [x.record for x in current_user.favorites]
for r in rows:
record = session.get(Record, r[2])
loc_list = [x.display_name for x in record.named_areas]
loc_list = [x.named_area.display_name for x in record.named_area_maps]
if loc_text := record.locality_text:
loc_list.append(loc_text)
collector = ''
Expand Down Expand Up @@ -641,7 +641,6 @@ def record_create(collection_name):
@admin.route('/records/<int:record_id>', methods=['GET', 'POST', 'DELETE'])
@login_required
def record_form(record_id):
print(current_app.root_path, admin.root_path, flush=True)
if record := session.get(Record, record_id):
if request.method == 'GET':
return render_template(
Expand Down
17 changes: 16 additions & 1 deletion app/blueprints/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ def get_assertion_type_option_list():

return jsonify(make_query_response(query))


def get_record_parts(record_id, part):
if record := session.get(Record, record_id):
ret = {}
if part == 'named-areas':
all_list = record.get_named_area_list()
for name, lst in all_list.items():
ret[name] = [x.to_dict() for x in lst]
return jsonify(ret)

return jsonify({})


#@api.route('/occurrence', methods=['GET'])
def get_occurrence():
# required
Expand Down Expand Up @@ -601,7 +614,7 @@ def get_occurrence():

na_list = []
if record := r[11]:
if named_areas := record.get_sorted_named_area_list():
if named_areas := record.get_named_area_list('legacy'):
na_list = [x.display_name for x in named_areas]
if x:= record.locality_text:
na_list.append(x)
Expand Down Expand Up @@ -704,4 +717,6 @@ def get_occurrence():
api.add_url_rule('/named-areas/<int:id>', 'get-named-area-detail', get_named_area_detail, ('GET'))
api.add_url_rule('/area-classes', 'get-area-class-list', get_area_class_list, ('GET'))
api.add_url_rule('/area-classes/<int:id>', 'get-area-class-detail', get_area_class_detail, ('GET'))

api.add_url_rule('/record/<int:record_id>/<part>', 'get-record-parts', get_record_parts, ('GET'))
api.add_url_rule('/occurrence', 'get-occurrence', get_occurrence) # for TBIA
4 changes: 2 additions & 2 deletions app/blueprints/static_admin/record.dev.js

Large diffs are not rendered by default.

45 changes: 36 additions & 9 deletions app/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ class Collection(Base, TimestampMixin):
organization = relationship('Organization', back_populates='collections')


record_named_area_map = Table(
'record_named_area_map',
Base.metadata,
Column('record_id', ForeignKey('record.id'), primary_key=True),
Column('named_area_id', ForeignKey('named_area.id'), primary_key=True)
)
#record_named_area_map = Table(
class RecordNamedAreaMap(Base):
__tablename__ = 'record_named_area_map'
record_id = Column(Integer, ForeignKey('record.id'), primary_key=True)
named_area_id = Column(Integer, ForeignKey('named_area.id'), primary_key=True)
via = Column(String(10))
record = relationship('Record', back_populates='named_area_maps')
named_area = relationship('NamedArea', back_populates='record_maps')


class Record(Base, TimestampMixin):
__tablename__ = 'record'
Expand Down Expand Up @@ -155,7 +158,9 @@ class Record(Base, TimestampMixin):
project_id = Column(Integer, ForeignKey('project.id'))

#named_area_relations = relationship('CollectionNamedArea')
named_areas = relationship('NamedArea', secondary=record_named_area_map, backref='entities')
#named_areas = relationship('NamedArea', secondary='record_named_area_map', back_populates='record')
#named_areas = relationship('Record', secondary='record_named_area_map', back_populates='records')
named_area_maps = relationship('RecordNamedAreaMap', back_populates='record')
assertions = relationship('RecordAssertion')
# assertions = relationship('EntityAssertion', secondary=entity_assertion_map, backref='entities')
project = relationship('Project')
Expand Down Expand Up @@ -194,8 +199,30 @@ def validate_altitude2(self, key, value):
def __repr__(self):
return '<Record id="{}">'.format(self.id)

def get_sorted_named_area_list(self):
return sorted(self.named_areas, key=lambda x: x.area_class.sort)
def get_named_area_list(self, list_name=''):

# TODO: list_name from setting
list_name_map = {
'default': [7, 8, 9, 10, 5, 6],
'legacy': [1, 2, 3, 4, 5, 6],
'hast-label': [7, 8, 9, 5, 6],
}
if list_name == '':
ret = {}
for x in list_name_map:
ret[x] = []
for m in self.named_area_maps:
if m.named_area.area_class_id in list_name_map[x]:
ret[x].append(m.named_area)
ret[x] = sorted(ret[x], key=lambda x: x.area_class.sort)
return ret
else:
if area_class_ids := list_name_map.get(list_name):
na_list = []
for m in self.named_area_maps:
if m.named_area.area_class_id in area_class_ids:
na_list.append(m.named_area)
return sorted(na_list, key=lambda x: x.area_class.sort)

def get_assertion(self, type_name='', part=''):
if type_name:
Expand Down
5 changes: 5 additions & 0 deletions app/models/gazetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class NamedArea(Base, TimestampMixin):
parent = relationship('NamedArea', back_populates='children', remote_side=[id])
#parent = relationship('NamedArea', foreign_keys=[parent_id])
pids = relationship('PersistentIdentifierNamedArea')
#records = relationship('Record', secondary='record_named_area_map', back_populates='named_areas')
record_maps = relationship('RecordNamedAreaMap', back_populates='named_area')

def __str__(self):
return f'<NamedArea: [{self.area_class.name}]{self.name}|{self.name_en}>'

@property
def display_name(self):
Expand Down
Loading

0 comments on commit 10b5499

Please sign in to comment.