Rails 并将多边形数据 (geojson) 插入到 postgis table
Rails and inserting polygon data (geojson) to the postgis table
我正在努力将 geoJSON 多边形数据插入 postgis table。 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结构:
-- 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;
有什么想法吗?
您的 SQL 查询有问题。首先,colums
的名称与 SQL 语法不匹配。第二,参数polygon
不正确,必须用函数transformgrid
对srid
进行变换,例如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');
我正在努力将 geoJSON 多边形数据插入 postgis table。 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结构:
-- 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;
有什么想法吗?
您的 SQL 查询有问题。首先,colums
的名称与 SQL 语法不匹配。第二,参数polygon
不正确,必须用函数transformgrid
对srid
进行变换,例如ST_SetSRID
。您可以在 postgresql 示例
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');