我正在努力将geoJSON多边形数据插入postgis表中。 PostGis工作正常。我可以从PG客户端执行此查询,但它不能在rails中的应用程序中运行。
我的模型中的插入查询在rails中:
class Route < ActiveRecord::Base
def self.AddNew(id_partner, route_name, polygon, active)
sql = "INSERT INTO routes('idPartner', 'name', 'polygon', 'active') VALUES ('#{id_partner}', '#{route_name}', ST_GeomFromGeoJSON('#{polygon}'),4326), '#{active}')"
ActiveRecord::Base.connection.execute(sql)
end
end
我不断得到:
PG::SyntaxError: ERROR: syntax error at or near "'idPartner'" LINE 1: INSERT INTO routes('idPartner', 'name', 'polygon', 'active')... ^ : INSERT INTO routes('idPartner', 'name', 'polygon', 'active') VALUES ('8', 'Dara', ST_GeomFromGeoJSON('{"type":"POLYGON","id":null,"coordinates":[[[19.00634765625,52.736291655910925],[22.1484375,52.133488040771475],[22.236328125,52.8823912222619]]]}'),4326), '1')
表格结构:
-- Table: public.routes
CREATE TABLE public.routes
(
id integer NOT NULL DEFAULT nextval('routes_id_seq'::regclass),
"idPartner" integer,
name character varying,
polygon geometry(Geometry,4326),
active integer,
CONSTRAINT routes_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.routes
OWNER TO postgres;
有什么想法吗?
答案 0 :(得分:1)
您的SQL查询存在问题。首先,colums
的名称与SQL语法不匹配。其次,参数polygon
不正确,您必须使用函数transform srid
转换grid
,例如ST_SetSRID
。您可以通过在postgresql示例中运行SQL来检查
INSERT INTO routes("idPartner", name, polygon, active) VALUES ('8', 'Dara', ST_SetSRID(ST_GeomFromGeoJSON(
'{"type":"Polygon","coordinates":[[[19.00634765625,52.736291655910925],[22.1484375,52.133488040771475],[22.236328125,52.8823912222619]]]}'
),4326), '1');