如何使用原始查询添加点?

时间:2016-02-16 11:10:40

标签: postgresql postgis laravel-5.2

我需要在postgres类型的表格中插入一个点,我试过了:

\DB::statement("SET search_path = postgis, public;");
\DB::statement("INSERT INTO points (latlong) VALUES( ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));");

但我收到了错误:

SQLSTATE[42883]: Undefined function: 7 ERROR: function st_geomfromtext(unknown, integer) does not exist
LINE 1: INSERT INTO points (latlong) VALUES(ST_GeomFromText('POINT(-...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. (SQL: INSERT INTO points (latlong) VALUES(ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));)

我正在使用postgres 9.4.5并安装了postgis ..任何想法?

1 个答案:

答案 0 :(得分:2)

  1. 使用CREATE EXTENSION IF NOT EXISTS postgis
  2. 之类的东西安装PostGIS
  3. 通过格式化ST_GeomFromText的WKT字符串来创建几何图形不是制作几何图形的最佳方法(更慢,更有损耗,更容易出错等),除非你的源数据已经是文本。要从两个浮点值创建点几何,请使用以下内容:

    DB::insert('INSERT INTO points (latlong) VALUES(ST_SetSRID(ST_MakePoint(?, ?), 4326))',
               [lng, lat]);