问题描述
我用sequelize-auto提取了一些PostGis层的模型,给出了:
I have extracted models of some PostGis layers with sequelize-auto, giving:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('table', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
geom: {
type: DataTypes.GEOMETRY('POINT', 4326),
allowNull: true,
},
...
在GET sequelize上,将geom作为GeoJSON发送给客户端:
On GET sequelize sends the geom to the client as GeoJSON:
{
"type":"Point",
"coordinates":[11.92164103734465,57.67219297300486]
}
当我尝试通过以下方式保存此PosGis错误时:
When I try to save this back PosGis errors with:
ERROR: Geometry SRID (0) does not match column SRID (4326)
该答案为如何添加SRID提供了一个指示(),
This answer gives a god indication of how to add the SRID (How to insert a PostGIS GEOMETRY Point in Sequelize ORM?),
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) {
...
});
我了解SRID曾经是从sequelize中删除的功能( https://github .com/sequelize/sequelize/issues/4054 ).
I understand that SRID used to be a feature that was removed from sequelize (https://github.com/sequelize/sequelize/issues/4054).
有人知道钩住Sequelize的方法,以便将srid添加到发送给PostGis的GeoJson中吗?放在哪里?在模型的设定者中?
Does anyone know a way to hook into Sequelize so that the srid is added to the GeoJson sent to PostGis? Where to put it? In a setter on the model?
推荐答案
在保存GEOMETRY
字段时,Sequelize似乎没有保存SRID(请注意,它正确地将SRID保存在GEOGRAPHY
字段上).然后,当您将来更新该模型时,更新将失败,因为它没有在GEOMETRY
字段中设置模型定义中预期的SRID.
It seems that Sequelize does not save the SRID when saving a GEOMETRY
field (note, that it correctly saves the SRID on a GEOGRAPHY
field). Then when you update that model in the future, the update will fail because it does not have a SRID set on the GEOMETRY
field as expected in the model definition.
这不是理想的解决方案,但是您可以使用Sequelize的beforeSave
挂钩始终指定要使用的坐标系.
This isn't a desirable solution, but you can use Sequelize's beforeSave
hook to always speciify the coordinates system to use.
myDatabase.define('user', {
name: {
type: Sequelize.STRING
},
geometry: {
type: Sequelize.GEOMETRY('POINT', 4326)
}
}, {
hooks: {
beforeSave: function(instance) {
if (instance.geometry && !instance.geometry.crs) {
instance.geometry.crs = {
type: 'name',
properties: {
name: 'EPSG:4326'
}
};
}
}
}
});
这篇关于使用Sequelize ORM插入/更新PostGis几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!