我正在构建一个也处理几何的简单API(存储到postGIS中)。 我使用knex-postgis通过knex访问postGIS中的ST_空间函数。
我使用这个例子来插入一个点,它在硬编码时起作用。 但是我缺乏经验让我感到不安,我怎样才能使查询动态化?我想创建一个带有x和y值输入的表单,并将其发送到ST_geomFromText函数以将其保存到db中的geom类型。 这是你在使用参数的时候吗?有人能指出我正确的方向吗?
// insert a point
const sql1 = db.insert({
id: 1,
geom: st.geomFromText('Point(0 0)', 4326)
}).into('points').toString();
console.log(sql1);
// insert into "points" ("geom", "id") values
(ST_geomFromText('Point(0 0)'), '1')
到目前为止,我已经尝试了
router.post('/', (req, res, next) => {
queries.create(req.body).then(poi => {
res.json(poi[0]);
});
});
插入查询
create(poi) {
const sql = db.insert(poi).returning('*').into('poi');
return sql;
},
在邮递员中,我将其发送到正文
{
"place": "test",
"comments": "new",
"numbers": 6,
"geom": "st.geomFromText('Point(-71.064544 44.28787)', 4326)"
}
但是得到一个错误“未处理的拒绝错误:解析错误 - 无效的几何体” 硬编码对象看起来一样,工作正常。
我有一种感觉,我使用st.geomFromText错误,但我不确定吗?
如果我的console.log返回查询
,这就是我得到的insert into "poi" ("comments", "geom", "numbers", "place") values ('new', 'st.geomFromText(''Point(-71.064544 44.28787)'', 4326)', 6, 'test')
(看它怎么不改变st.geom ..到st_geom ..?还有ST函数周围的引号和Point对象不对)
当我在pgAdmin
中运行时,此字符串有效insert into "poi" ("comments", "geom", "numbers", "place") values ('new', st_GeomFromText('Point(-71.064544 44.28787)', 4326), 6, 'test')
编辑: 我也控制了。硬编码的版本。它似乎没有在几何值
上添加额外的''insert into "poi" ("geom") values (ST_geomFromText('Point(-71.064544 44.28787)', 4326))
有什么建议吗?
答案 0 :(得分:1)
您将postman 中的坐标作为JSON对象中的字符串值
传递"geom": "st.geomFromText('Point(-71.064544 44.28787)', 4326)"
并希望您的代码将其转换为函数调用
st.geomFromText('Point(-71.064544 44.28787)', 4326)
在这种情况下knex
做了什么。它需要字符串并将其转换为字符串字段(此处为Postgres {{{{{{{{{{{{{{ {3}})。
你能做什么。将几何图形传递给您的API
'
在您的路线处理程序
中''