将 POINT 插入 postgres 数据库

问题描述 投票:0回答:5

我需要在 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 吗?

postgresql postgis node-postgres
5个回答
13
投票

如果您“直接”编写 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)

11
投票

经过几次组合,发现这个有效。!!

( '(' + 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));
是行不通的。


5
投票

我最近在使用

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 ]);

3
投票

当前版本(Postgres 12,第 8 页)应该可以简单地使用 Postgres 的POINT 函数 来设置点列值。

例子:

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}

1
投票

如果您使用的是 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]);

另见:几何构造函数.

© www.soinside.com 2019 - 2024. All rights reserved.