将POINT插入postgres数据库

时间:2017-01-05 20:35:39

标签: postgresql postgis node-postgres

我需要在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. point(req.body.places.point.x,req.body.places.point.y)
  3. point&#39;(2,1)&#39;
  4. 以上所有抛出错误。我需要使用POSTGIS吗?

5 个答案:

答案 0 :(得分:5)

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

( '(' + 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));不起作用。

答案 1 :(得分:5)

如果你直接编写SQL&#34;

,这是有效的
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 :(得分: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]);

另请参阅:Geometry Constructors

答案 3 :(得分:0)

我最近在插入具有地理位置(点)列的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 :(得分:0)

当前版本(Postgres 12,第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}
相关问题