【问题标题】:How to insert value to a column as a result of geometry of another column in the same table由于同一表中另一列的几何形状,如何将值插入列
【发布时间】:2021-12-22 05:16:40
【问题描述】:

我在下面发布了数据库表。两列geometryOfCellRepresentativeToTreatmentgeometryOfCellRepresentativeToBuffer 是几何类型。并且它们的值等于列的几何形状fourCornersRepresentativeToTreatmentAsGeoJSON_fourCornersRepresentativeToBufferAsGeoJSON 分别。 如何将值作为前列的几何形状插入到后列中

表格

CREATE TABLE grid_cell_data (
           id SERIAL PRIMARY KEY,
           isTreatment boolean,
           isBuffer boolean,
           fourCornersRepresentativeToTreatmentAsGeoJSON text,
           fourCornersRepresentativeToBufferAsGeoJSON text,
           distanceFromCenterPointOfTreatmentToNearestEdge numeric,
           distanceFromCenterPointOfBufferToNearestEdge numeric,
           areasOfCoveragePerWindowForCellsRepresentativeToTreatment numeric,
           areasOfCoveragePerWindowForCellsRepresentativeToBuffer numeric,
           averageHeightsPerWindowRepresentativeToTreatment numeric,
           averageHeightsPerWindowRepresentativeToBuffer numeric,
           geometryOfCellRepresentativeToTreatment geometry,
           geometryOfCellRepresentativeToBuffer geometry)

data_to_be_inserted

isTreatment = True//boolean
        isBuffer = False //boolean
        fourCornersRepresentativeToTreatmentAsGeoJSON_ = json.dumps(fourCornersOfKeyWindowAsGeoJSON[i])//string
        fourCornersRepresentativeToBufferAsGeoJSON_ = None//string
        distanceFromCenterPointOfTreatmentToNearestEdge_ = distancesFromCenterPointsToNearestEdge[i]
        distanceFromCenterPointOfBufferToNearestEdge_ = None
        areasOfCoveragePerWindowForCellsRepresentativeToTreatment_= areasOfCoveragePerWindow[i]
        areasOfCoveragePerWindowForCellsRepresentativeToBuffer_ = None
        averageHeightsPerWindowRepresentativeToTreatment_ = averageHeightsPerWindow[i]
        averageHeightsPerWindowRepresentativeToBuffer_ = None
        geometryOfCellRepresentativeToTreatment_ = //geometry of fourCornersRepresentativeToTreatmentAsGeoJSON_
        geometryOfCellRepresentativeToBuffer_ = //geometry of fourCornersRepresentativeToBufferAsGeoJSON_

图片

【问题讨论】:

    标签: sql postgresql sql-update postgis


    【解决方案1】:

    只需将geojson 字符串设置为UPDATE 语句中的geometry 列(为了更明确,将:: 字符串转换为geometry):

    UPDATE grid_cell_data SET
      geometryOfCellRepresentativeToTreatment = fourCornersRepresentativeToTreatmentAsGeoJSON::geometry,
      geometryOfCellRepresentativeToBuffer = fourCornersRepresentativeToBufferAsGeoJSON::geometry;
    

    代码

    UPDATE grid_cell_data set 
        geometryOfCellRepresentativeToTreatment = ST_GeomFromGeoJSON(fourCornersRepresentativeToTreatmentAsGeoJSON)
     WHERE 
        fourCornersRepresentativeToTreatmentAsGeoJSON <> '' and fourCornersRepresentativeToTreatmentAsGeoJSON IS NOT NULL;
    
    UPDATE grid_cell_data SET
        geometryOfCellRepresentativeToBuffer = ST_GeomFromGeoJSON(fourCornersRepresentativeToBufferAsGeoJSON)
    WHERE 
        fourCornersRepresentativeToBufferAsGeoJSON <> '' and fourCornersRepresentativeToBufferAsGeoJSON IS NOT NULL;
    

    小提琴链接fiddle code

    注意:您在同一条记录中存储了两次相同的几何图形,这并不是必需的。您应该这样存储几何图形,并且仅按需以您想要的格式将它们序列化,例如WKT、KML、GeoJSON 等

    【讨论】:

    • 请如上图所示,对于某些行,fourCornersRepresentativeToTreatmentAsGeoJSON 具有价值,而对于其他行则没有。同样适用于fourCornersRepresentativeToBufferAsGeoJSON。因此,当我使用更新语句时,我收到无效的几何图形。我认为这是由于有时fourCornersRepresentativeToTreatmentAsGeoJSON和fourCornersRepresentativeToBufferAsGeoJSON有价值,有时没有。请告诉我如何更改更新语句适合这种情况??
    • 在这种情况下,应该没有几何相关。
    • 是的,换句话说,如果fourCornersRepresentativeToTreatmentAsGeoJSON为空,那么geometryOfCellRepresentativeToTreatment也是空的,fourCornersRepresentativeToBufferAsGeoJSON和geometryOfCellRepresentativeToBuffer也是如此
    • 我想通知您,您提供的代码运行良好且合乎逻辑。但直到我在 where 子句中添加了fourCornersRepresentativeToBufferAsGeoJSON '' 之前它并没有给我想要的结果。所以 where 子句变成了WHERE fourCornersRepresentativeToBufferAsGeoJSON &lt;&gt; '' and fourCornersRepresentativeToBufferAsGeoJSON IS NOT NULL; 我只想告诉你。非常感谢......我可以更新你的答案吗?你想做吗?
    • 您能帮我找到这个问题的解决方案吗:stackoverflow.com/questions/69939684/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 2014-09-19
    • 2017-07-17
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多