【问题标题】:Insert POINT into postgres database将 POINT 插入 postgres 数据库
【发布时间】:2017-05-20 12:54:44
【问题描述】:

我需要在 postgres 数据库中插入/更新点列类型。

我正在使用node-postgres

使用 POSTGRES 管理面板生成的脚本将更新查询显示为

UPDATE public.places SET id=?, user_id=?, business_name=?, alternate_name=?, primary_category=?, categories=?, description=?, address=?, city=?, state=?, country=?, zip=?, point WHERE <condition>;

如何从经纬度获得点?

我已经看到几个使用 POSTGIS 的答案,但无法使其正常工作。

在 POSTGRES (https://www.postgresql.org/docs/9.2/static/xfunc-sql.html) 的文档中提到我们可以使用 point '(2,1)',但这不适用于 pg 查询。

我现在拥有的:

var config = {
  user: 'postgres',
  database: 'PGDATABASE',
  password: 'PGPASSWORD!',
  host: 'localhost',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
};

还有更新部分:

app.post('/updatePlaces', function(req, res, next) {
    console.log("Update");
    console.log(req.body.places);
    pool.query('UPDATE places SET address = $1, alternate_name = $2, business_name = $3, categories = $4, city = $5, country = $6, description = $7, point = $8, primary_category = $9, state = $10, zip = $11', [req.body.places.address, req.body.places.alternate_name, req.body.places.business_name, req.body.places.categories, req.body.places.city, req.body.places.country, req.body.places.description, (req.body.places.point.x, req.body.places.point.y), req.body.places.primary_category, req.body.places.state, req.body.places.zip], function(err, result) {
      if(err) {
          console.log(err);
          return err;
      }

      res.send(result.rows[0]);
    });
});

尝试了许多不同的方法来通过点:

  1. (req.body.places.point.x, req.body.places.point.y)
  2. 点(req.body.places.point.x, req.body.places.point.y)
  3. 点'(2,1)'

以上所有抛出错误。我需要使用 POSTGIS 吗?

【问题讨论】:

  • 使用pg-promise时可以自动完成。如果您有兴趣,那么我将通过示例添加答案;)
  • 是的,请@vitaly-t

标签: postgresql postgis node-postgres


【解决方案1】:

如果您“直接”编写 SQL,这将有效:

CREATE TEMP TABLE x(p point) ;
INSERT INTO x VALUES ('(1,2)');
INSERT INTO x VALUES (point(3, 4));
SELECT * FROM x ;

结果

(1,2)
(3,4)

【讨论】:

    【解决方案2】:

    经过几次组合,发现这是可行的。!

    ( '(' + req.body.places.point.x + ',' + req.body.places.point.y +')' )

    如果有人尝试仅使用 node-postgres 执行此操作,则发布为答案。

    所以你可以使用单引号:insert into x values ( '(1,2)' );

    但是在查询中使用insert into x values (point(1,2)); 不起作用。

    【讨论】:

      【解决方案3】:

      我最近在使用node-postgres 插入带有地理(点)列的postgis 数据库时遇到了类似的问题。我的解决方案是使用:

      pool.query("INSERT INTO table (name, geography) VALUES ($1, ST_SetSRID(ST_POINT($2, $3), 4326))",
            [req.body.name, req.body.lat, req.body.lng ]);
      

      【讨论】:

        【解决方案4】:

        如果您使用pg-promise,则可以自动格式化自定义类型,请参阅Custom Type Formatting

        你可以像这样引入自己的类型:

        function Point(x, y) {
            this.x = x;
            this.y = y;
        
            // Custom Type Formatting:
            this._rawDBType = true; // to make the type return the string without escaping it;
        
            this.formatDBType = function () {
                return 'ST_MakePoint(' + this.x + ',' + this.y + ')';
            };
        }
        

        在某些时候你会创建你的对象:

        var p = new Point(11, 22);
        

        然后你就可以使用像常规类型这样的变量了:

        db.query('INSERT INTO places(place) VALUES(ST_SetSRID($1, 4326))', [p]);
        

        另请参阅:Geometry Constructors

        【讨论】:

        • 谢谢@vitaly-t 我还有一个问题,我的数据库更多是面向位置的,我应该使用 postGIS 吗?
        • 是的,这通常是个好主意,因为如今 PostGIS 就像 PostgreSQL 的原生组件,提供了基于位置的搜索所需的所有智能搜索;)
        • 酷。谢谢你。 :)
        【解决方案5】:

        当前版本(Postgres 12,pg 8)应该可以使用 Postgres 的 POINT function 设置点列值。

        例子:

        export async function setPoint(client, x, y, id) {
            const sql = `UPDATE table_name SET my_point = POINT($1,$2) WHERE id = $3 RETURNING my_point`;
            const result = await client.query(sql, [x, y, id]);
            return result.rows[0];
        }
        await setPoint(client, 10, 20, 5);
        

        结果:

        {x: 10.0, y: 20.0}
        

        【讨论】:

          猜你喜欢
          • 2014-01-12
          • 1970-01-01
          • 1970-01-01
          • 2020-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-03
          • 1970-01-01
          相关资源
          最近更新 更多