如何在Sequelize ORM中插入PostGIS GEOMETRY点? [英] How to insert a PostGIS GEOMETRY Point in Sequelize ORM?
问题描述
我正在尝试在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的答案, 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屋!