123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- """
- 农田数据模型
- 定义Farmland_data表的ORM模型,用于存储耕地样点空间位置与索引数据
- """
- from sqlalchemy import Column, Integer, Float, ForeignKey
- from geoalchemy2 import Geometry
- from app.database import Base
- class FarmlandData(Base):
- """
- 耕地样点空间位置与索引数据模型
-
- @param {int} farmland_id - 区域农业用地矢量点编号(主键)
- @param {int} sample_id - 采样自增的ID(主键+外键+自增)
- @param {float} lon - 经度
- @param {float} lan - 纬度
- @param {float} type - 用地类型,旱地(0)、水田(1)、水浇地(2)
- @param {geometry} geom - 点几何对象(使用PostGIS,基于经纬度生成)
- """
- __tablename__ = 'Farmland_data'
- __table_args__ = {'comment': '耕地样点空间位置与索引数据表,存储农业用地的坐标、类型和空间几何信息'}
-
- # 主键字段 - 保持与原表结构完全一致的大小写
- farmland_id = Column('Farmland_ID', Integer, primary_key=True, comment='区域农业用地矢量点编号')
- sample_id = Column('Sample_ID', Integer, primary_key=True, autoincrement=True, comment='采样自增的ID')
-
- # 地理坐标字段 - 保持原始大小写
- lon = Column('lon', Float, nullable=True, comment='经度')
- lan = Column('lan', Float, nullable=True, comment='纬度')
-
- # 用地类型字段 - 保持原始大小写
- type = Column('Type', Float, nullable=True, comment='用地类型:旱地(0)、水田(1)、水浇地(2)')
-
- # 使用PostGIS几何字段存储点位置(可选,便于空间查询)
- geom = Column(Geometry('POINT', srid=4326), comment='点几何对象')
-
- def __repr__(self):
- return f"<FarmlandData(farmland_id={self.farmland_id}, sample_id={self.sample_id}, " \
- f"lon={self.lon}, lan={self.lan}, type={self.type})>"
-
- @property
- def land_type_name(self):
- """
- 获取用地类型名称
-
- @returns {str} 用地类型名称
- """
- type_mapping = {
- 0.0: "旱地",
- 1.0: "水田",
- 2.0: "水浇地"
- }
- return type_mapping.get(self.type, "未知类型")
-
- def to_dict(self):
- """
- 转换为字典格式
-
- @returns {dict} 包含所有字段的字典
- """
- return {
- 'farmland_id': self.farmland_id,
- 'sample_id': self.sample_id,
- 'lon': self.lon,
- 'lan': self.lan,
- 'type': self.type,
- 'land_type_name': self.land_type_name,
- 'geom': str(self.geom) if self.geom else None
- }
|