farmland.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. 农田数据模型
  3. 定义Farmland_data表的ORM模型,用于存储耕地样点空间位置与索引数据
  4. """
  5. from sqlalchemy import Column, Integer, Float, ForeignKey
  6. from geoalchemy2 import Geometry
  7. from app.database import Base
  8. class FarmlandData(Base):
  9. """
  10. 耕地样点空间位置与索引数据模型
  11. @param {int} farmland_id - 区域农业用地矢量点编号(主键)
  12. @param {int} sample_id - 采样自增的ID(主键+外键+自增)
  13. @param {float} lon - 经度
  14. @param {float} lan - 纬度
  15. @param {float} type - 用地类型,旱地(0)、水田(1)、水浇地(2)
  16. @param {geometry} geom - 点几何对象(使用PostGIS,基于经纬度生成)
  17. """
  18. __tablename__ = 'Farmland_data'
  19. __table_args__ = {'comment': '耕地样点空间位置与索引数据表,存储农业用地的坐标、类型和空间几何信息'}
  20. # 主键字段 - 保持与原表结构完全一致的大小写
  21. farmland_id = Column('Farmland_ID', Integer, primary_key=True, comment='区域农业用地矢量点编号')
  22. sample_id = Column('Sample_ID', Integer, primary_key=True, autoincrement=True, comment='采样自增的ID')
  23. # 地理坐标字段 - 保持原始大小写
  24. lon = Column('lon', Float, nullable=True, comment='经度')
  25. lan = Column('lan', Float, nullable=True, comment='纬度')
  26. # 用地类型字段 - 保持原始大小写
  27. type = Column('Type', Float, nullable=True, comment='用地类型:旱地(0)、水田(1)、水浇地(2)')
  28. # 使用PostGIS几何字段存储点位置(可选,便于空间查询)
  29. geom = Column(Geometry('POINT', srid=4326), comment='点几何对象')
  30. def __repr__(self):
  31. return f"<FarmlandData(farmland_id={self.farmland_id}, sample_id={self.sample_id}, " \
  32. f"lon={self.lon}, lan={self.lan}, type={self.type})>"
  33. @property
  34. def land_type_name(self):
  35. """
  36. 获取用地类型名称
  37. @returns {str} 用地类型名称
  38. """
  39. type_mapping = {
  40. 0.0: "旱地",
  41. 1.0: "水田",
  42. 2.0: "水浇地"
  43. }
  44. return type_mapping.get(self.type, "未知类型")
  45. def to_dict(self):
  46. """
  47. 转换为字典格式
  48. @returns {dict} 包含所有字段的字典
  49. """
  50. return {
  51. 'farmland_id': self.farmland_id,
  52. 'sample_id': self.sample_id,
  53. 'lon': self.lon,
  54. 'lan': self.lan,
  55. 'type': self.type,
  56. 'land_type_name': self.land_type_name,
  57. 'geom': str(self.geom) if self.geom else None
  58. }