Skip to content

Commit

Permalink
gazetter
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed Feb 29, 2024
1 parent 4df928b commit 8898f9b
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 192 deletions.
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
datefmt = %H:%M:%S
12 changes: 10 additions & 2 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from alembic import context

from geoalchemy2 import alembic_helpers

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
Expand All @@ -21,7 +23,7 @@
from app.models.site import *
from app.models.taxon import *
from app.models.collection import *
from app.models.addon import *
from app.models.gazetter import *

from app.database import Base
target_metadata = Base.metadata
Expand Down Expand Up @@ -50,6 +52,9 @@ def run_migrations_offline():
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)

with context.begin_transaction():
Expand All @@ -71,7 +76,10 @@ def run_migrations_online():

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
connection=connection, target_metadata=target_metadata,
include_object=alembic_helpers.include_object,
process_revision_directives=alembic_helpers.writer,
render_item=alembic_helpers.render_item,
)

with context.begin_transaction():
Expand Down
1 change: 1 addition & 0 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Create Date: ${create_date}
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
import geoalchemy2

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
Expand Down
39 changes: 39 additions & 0 deletions alembic/versions/5b41d404cb3b_add_country.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""add-country
Revision ID: 5b41d404cb3b
Revises: 675f3d3e99af
Create Date: 2024-02-23 19:31:48.163498
"""
from alembic import op
import sqlalchemy as sa
from geoalchemy2 import Geometry
import geoalchemy2

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


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('country',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name_en', sa.String(length=500), nullable=True),
sa.Column('name_zh', sa.String(length=500), nullable=True),
sa.Column('continent', sa.String(length=500), nullable=True),
sa.Column('iso3166_1', sa.String(length=2), nullable=True),
sa.Column('iso3', sa.String(length=3), nullable=True),
sa.Column('status', sa.String(length=500), nullable=True),
sa.Column('sort', sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('country')
# ### end Alembic commands ###
31 changes: 31 additions & 0 deletions alembic/versions/675f3d3e99af_add_geom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""add-geom
Revision ID: 675f3d3e99af
Revises: fb2dd663057f
Create Date: 2024-02-23 18:36:09.844916
"""
from alembic import op
import sqlalchemy as sa
from geoalchemy2 import Geometry
import geoalchemy2

# revision identifiers, used by Alembic.
revision = '675f3d3e99af'
down_revision = 'fb2dd663057f'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_geospatial_column('named_area', sa.Column('geom_mpoly', Geometry(geometry_type='MULTIPOLYGON', srid=4326, spatial_index=False, from_text='ST_GeomFromEWKT', name='geometry'), nullable=True))
op.create_geospatial_index('idx_named_area_geom_mpoly', 'named_area', ['geom_mpoly'], unique=False, postgresql_using='gist', postgresql_ops={})
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_geospatial_index('idx_named_area_geom_mpoly', table_name='named_area', postgresql_using='gist', column_name='geom_mpoly')
op.drop_geospatial_column('named_area', 'geom_mpoly')
# ### end Alembic commands ###
18 changes: 12 additions & 6 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
Record,
RecordAssertion,
AssertionType,
AreaClass,
NamedArea,
Project,
Unit,
UnitAssertion,
Expand All @@ -53,6 +51,10 @@
Taxon,
MultimediaObject,
)
from app.models.gazetter import (
AreaClass,
NamedArea,
)
from app.models.site import (
User,
UserList,
Expand Down Expand Up @@ -98,7 +100,7 @@ def save_record(record, data, is_create=False):
# print(c['name'], c['type'], flush=True)
#print(columns_table, '-----------',flush=True)

#print(data, flush=True)
print(data, flush=True)

if is_create is True:
session.add(record)
Expand Down Expand Up @@ -142,7 +144,10 @@ def save_record(record, data, is_create=False):
name_key = name.replace('__hidden_value', '_id')
setattr(record, name_key, value)

elif match := re.search(r'^(named_areas|record_assertions)__(.+)__hidden_value', name):
elif name == 'named_area_ids' and value:
m2m['named_areas'] = value.split(',')

elif match := re.search(r'^(record_assertions)__(.+)__hidden_value', name):
# print(match, match.group(1), match.group(2), value,flush=True)
if value:
name_class = match.group(1)
Expand Down Expand Up @@ -176,13 +181,14 @@ def save_record(record, data, is_create=False):
o2m[name_class][num]['annotation'][annotation_part] = {}
o2m[name_class][num]['annotation'][annotation_part] = value

named_areas = []
# print(m2m, flush=True)
named_areas = []
for i in m2m['named_areas']:
# only need id
if obj := session.get(NamedArea, int(i[1])):
if obj := session.get(NamedArea, int(i)):
named_areas.append(obj)


assertions = []
for i in m2m['record_assertions']:
type_id = int(i[0])
Expand Down
6 changes: 4 additions & 2 deletions app/blueprints/admin_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
)
from app.models.collection import (
AssertionType,
AreaClass,
NamedArea,
Collection,
Person,
Transaction,
AnnotationType,
Annotation,
MultimediaObjectAnnotation,
)
from app.models.gazetter import (
NamedArea,
AreaClass,
)
from app.models.taxon import (
Taxon,
)
Expand Down
34 changes: 31 additions & 3 deletions app/blueprints/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
join,
)
from sqlalchemy.dialects.postgresql import ARRAY
from geoalchemy2.functions import (
ST_Point,
ST_SetSRID,
ST_Within,
)

from app.database import session

from app.models.collection import (
Record,
Person,
NamedArea,
AreaClass,
Unit,
Identification,
Person,
Expand All @@ -44,6 +47,10 @@
#LogEntry,
#get_structed_list,
)
from app.models.gazetter import (
NamedArea,
AreaClass,
)
from app.models.taxon import (
Taxon,
TaxonRelation,
Expand Down Expand Up @@ -360,7 +367,7 @@ def get_named_area_detail(id):

#@api.route('/named_areas', methods=['GET'])
def get_named_area_list():
query = NamedArea.query
query = NamedArea.query.join(AreaClass)
if filter_str := request.args.get('filter', ''):
filter_dict = json.loads(filter_str)
if keyword := filter_dict.get('q', ''):
Expand All @@ -372,6 +379,25 @@ def get_named_area_list():
query = query.filter(NamedArea.area_class_id==area_class_id)
if parent_id := filter_dict.get('parent_id'):
query = query.filter(NamedArea.parent_id==parent_id)
if within := filter_dict.get('within'):
set_srid = 4326
if srid := within.get('srid'):
set_srid = srid
if point := within.get('point'):
if len(point) == 2:
query = query.filter(func.ST_Within(
func.ST_SetSRID(func.ST_Point(point[0], point[1]), set_srid),
NamedArea.geom_mpoly
))
query = query.order_by(AreaClass.sort, NamedArea.name_en)
else:
query = query.filter(NamedArea.id==0)

args_range = [0, 100]
if x := request.args.get('range'):
args_range = json.loads(x)

query = query.offset(args_range[0]).limit(args_range[1])

return jsonify(make_query_response(query))

Expand Down Expand Up @@ -437,10 +463,12 @@ def get_taxon_list():
query = query.slice(range_dict[0], range_dict[1])

#print(query, flush=True)
query = query.limit(100) # TODO
return jsonify(make_query_response(query))

def get_area_class_list():
query = AreaClass.query.order_by('sort')
query = query.filter(AreaClass.id > 4) # HACK
if filter_str := request.args.get('filter', ''):
filter_dict = json.loads(filter_str)
if keyword := filter_dict.get('q', ''):
Expand Down
4 changes: 3 additions & 1 deletion app/blueprints/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
RecordAssertion,
AssertionType,
AreaClass,
NamedArea,
Unit,
UnitAssertion,
Identification,
Person,
Taxon,
)
from app.models.gazetter import (
NamedArea,
)
from app.models.taxon import (
Taxon,
)
Expand Down
36 changes: 0 additions & 36 deletions app/models/addon.py

This file was deleted.

Loading

0 comments on commit 8898f9b

Please sign in to comment.