如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点? [英] How to insert a PostGIS GEOMETRY Point in Sequelize ORM?

查看:35
本文介绍了如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Sequelize.js ORM 中具有几何列的表中插入一行.我有纬度、经度和高度,需要先将其转换为一个点,以便将其作为几何图形插入.

I am trying to insert a row in a table that has a geometry column in Sequelize.js ORM. I have latitude, longitude and altitude and need to convert it to a point first so I can Insert it as a geometry.

进行转换的 PostGIS 存储过程是

The PostGIS stored procedure that does the converting is

ST_MakePoint( longitude, latitude, altitude ) 

要插入一行,我正在使用 sequelize model.create 函数

To Insert a row I am using the sequelize model.create function

models.Data.create({    
  location: "ST_MakePoint("+request.params.lon+", "+request.params.lat+", "+request.params.alt+")", // PSUEDO code, How can I call this function?
  speed: request.params.spd,
  azimuth: request.params.azi,
  accuracy: request.params.acc
});

现在我要做的是使字段 location 具有 "ST_MakePoint("+request.params.lon+", "+request.params.lat+","+request.params.alt+")" 当我插入行时.

Now what I want to do Is make the field location have the returned result of "ST_MakePoint("+request.params.lon+", "+request.params.lat+", "+request.params.alt+")" when I insert the row.

我该怎么做?

推荐答案

扩展 l0oky 的答案,集成测试 有很多关于如何将 json 与不同类型的几何体一起使用的好线索.基本上,sequelize 似乎会将提供的几何对象字符串化,假设它是有效的 GeoJSON 并将其传递到 PostGIS 函数 ST_GeomFromGeoJSON.因此,几何对象只需遵循 GeoJSON 规范.

Expanding on l0oky's answer, the integration test has a lot of good clues on how to use the json with varying types of Geometry. Basically, it appears that sequelize will stringify the provided geometry object assuming that it is valid GeoJSON and pipe that into the PostGIS function ST_GeomFromGeoJSON. Therefore, one can just follow the GeoJSON spec for geometry objects.

积分:

var point = { type: 'Point', coordinates: [39.807222,-76.984722]};

User.create({username: 'username', geometry: point }).then(function(newUser) {
...
});

线串:

var line = { type: 'LineString', 'coordinates': [ [100.0, 0.0], [101.0, 1.0] ] };

User.create({username: 'username', geometry: line }).then(function(newUser) {
...
});

多边形:

var polygon = { type: 'Polygon', coordinates: [
             [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
               [100.0, 1.0], [100.0, 0.0] ]
             ]};

User.create({username: 'username', geometry: polygon }).then(function(newUser) {
...
});

设置自定义 SRID:

var point = { 
  type: 'Point', 
  coordinates: [39.807222,-76.984722],
  crs: { type: 'name', properties: { name: 'EPSG:4326'} }
};

User.create({username: 'username', geometry: point }).then(function(newUser) {
...
});

这篇关于如何在 Sequelize ORM 中插入 PostGIS GEOMETRY 点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆