如何在 Sequelize ORM 中插入 PostGIS 几何点?

How to insert a PostGIS GEOMETRY Point in Sequelize ORM?

我正在尝试在 table 中插入一行,该行在 Sequelize.js ORM 中有一个几何列。 我有纬度、经度和高度,需要先将其转换为一个点,这样我才能将其作为几何插入。

执行转换的 PostGIS 存储过程是

ST_MakePoint( longitude, latitude, altitude ) 

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

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+")" 的结果。

我该怎么做?

经过一些研究,我发现 Sequelize 3.5.1(支持 GEOMETRY)有一个 test 插入了一个 Point.

var point = { type: 'Point', coordinates: [39.807222,-76.984722] }; 
return User.create({ username: 'user', email: ['foo@bar.com'], location: point})

其中 location 是一个 GEOMETRY 字段。这样我就不需要手动调用 ST_MakePoint,sequelize 会处理它。

扩展 l0oky 的回答,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 几何对象。

积分:

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) {
...
});