且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-06 21:26:30

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