Return geoapi store geojson

This commit is contained in:
phil 2023-12-17 12:20:07 +05:30
parent 4048e61221
commit 1b7db43ee7
4 changed files with 44 additions and 40 deletions

View file

@ -16,11 +16,12 @@ import shapely
import pyproj
from sqlmodel import SQLModel, Field
from sqlmodel.ext.asyncio.session import AsyncSession
from pydantic import BaseModel
from geoalchemy2.shape import from_shape
from sqlalchemy.dialects.postgresql import BIGINT
from sqlalchemy import BigInteger, Column, String, func, and_
from sqlalchemy import BigInteger, Column, MetaData, String, func, and_, text
from sqlalchemy.sql import sqltypes
from psycopg2.extensions import adapt
@ -36,13 +37,16 @@ from shapefile import (Writer as ShapeFileWriter,
POLYGON, POLYGONZ,
)
from ..database import db_session
from ..config import conf
from .models_base import Model
from ..models.metadata import survey, raw_survey
from ..utils import upsert_df
from .survey import Equipment, Surveyor, Accuracy
from .misc import Qml
from .category import Category
from .project import Project
from ..utils import upsert_df
LOCALE_DATE_FORMAT = locale.nl_langinfo(locale.D_FMT)
@ -138,6 +142,7 @@ class SurveyModel(BaseSurveyModel):
"""
Base mixin class for defining final (reprojected) survey data, with a status
"""
metadata: ClassVar[MetaData] = survey
status: str = Field(sa_type=String(1))
get_gdf_with_related: ClassVar[bool] = False
@ -198,7 +203,9 @@ class SurveyModel(BaseSurveyModel):
'] #' + df.index.astype('U')
@classmethod
async def get_geojson(cls, simplify_tolerance=0):
async def get_geojson(cls, registry=None, simplify_tolerance=0):
if registry is None:
from ..registry import registry
## Fastest, but the id is in properties and needs the front end (eg Gisaf for mapbox)
## to move it at the feature level
@ -211,7 +218,7 @@ class SurveyModel(BaseSurveyModel):
FROM (
SELECT f.geom,
f.id::varchar,
{description} || ' [' || '{model.category.group}' || '-' || '{model.category.minor_group_1}' || '] #' || f.id as popup,
{description} || ' [' || '{category.group}' || '-' || '{category.minor_group_1}' || '] #' || f.id as popup,
f.status
FROM "{schema}"."{table}" as f
WHERE f.geom is not null
@ -232,7 +239,7 @@ class SurveyModel(BaseSurveyModel):
'geometry', ST_AsGeoJSON(geom)::jsonb,
'properties', jsonb_build_object(
'popup',
{description} || ' [' || '{model.category.group}' || '-' || '{model.category.minor_group_1}' || '] #' || inputs.id::varchar,
{description} || ' [' || '{category.group}' || '-' || '{category.minor_group_1}' || '] #' || inputs.id::varchar,
'status', status
)
) AS feature
@ -241,16 +248,20 @@ class SurveyModel(BaseSurveyModel):
"""
sql_query_for_geojson = sql_query_for_geojson_fast
async with db.acquire(reuse=False) as conn:
category = registry.categories.loc[cls.category_name]
session: AsyncSession
async with db_session() as session:
query = sql_query_for_geojson.format(
model=cls,
schema=cls.__table_args__['schema'],
table=cls.__tablename__,
description=adapt(cls.category.description),
category=category,
schema=cls.metadata.schema,
#table=cls.__tablename__,
# FIXME: should be __tablename__, but see SQLModel.__tablename__ which use lower(__name__)
table=cls.__name__,
description=adapt(category.description),
)
result = await conn.scalar(query)
return result
result = await session.exec(text(query))
return result.first()[0]
def to_row(self):
@ -1048,6 +1059,7 @@ class RawSurveyBaseModel(BaseSurveyModel, GeoPointMModel):
Abstract base class for category based raw survey point models
"""
#__abstract__ = True
metadata: ClassVar[MetaData] = raw_survey
geom: Annotated[str, WKBElement] = Field(sa_type=Geometry('POINTZ', dimension=3,
srid=conf.geo.raw_survey.srid))
status: str = Field(sa_type=String(1))