egg+sequelize+postgis 空间数据新增

mac2025-12-07  10

一、建立空间表,建议使用下边的代码建立空间表不建议使用navicat去建表

CREATE TABLE city( id int4,name varchar(25), geom geometry(POINT,4326) );

二、egg中建立model

module.exports = app => { const { INTEGER, STRING, GEOMETRY } = app.Sequelize; const City = app.model.define("city", { id: { type: INTEGER, primaryKey: true, autoIncrement: true }, name: STRING, geom: GEOMETRY }, { freezeTableName: true, //直接查找设置的表名,默认是表名加s或者es timestamps: false, }); return City; }

 三、数据插入到空间表中

(1)直接执行sql命令如下

INSERT INTO city(id,name,geom)VALUES(0,'郑州',st_geomfromtext('SRID=4326;point(113.34 34.23)'))

(2) 使用sequelize插入空间数据

async addCityPoint() { const { model, request } = this.ctx let result = await model.City.create({ name: '1', geom: { type: 'Point', coordinates: [39.807222, -76.984722], crs: { type: 'name', properties: { name: 'EPSG:4326' } } } }) this.ctx.body = result }

 

 

 

最新回复(0)