supabase-js插入几何对象

发布于 2025-01-24 04:28:12 字数 1324 浏览 5 评论 0原文

我正在尝试将几何对象插入我的supabase Postgres表中。

有以下几何列:

列名称数据类型
点点line_string
linestringpolygon
polygon

我尝试了多种插入点对象的方法(我还没有进入线文或多边形,看到它们更复杂)。


const geometry = { x: -123, y: -123 }

const point = `(${geometry.x}, ${geometry.y})`; // Try number 1
const point = `point(${geometry.x},${geometry.y})`; // Try number 2
const point = `ST_SetSRID(ST_MakePoint(${geometry.y}, ${geometry.x}), 4326)`; // Try number 3

const { data, error } = await supabase.from('geometries').upsert({ point });

这三个都给出了相同的错误:

{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"(0" <-- parse error at position 2 within geometry'
}
{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"point(0, 0" <-- parse error at position 10 within geometry'
}
{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"ST" <-- parse error at position 2 within geometry'
}

如何插入/UPSERT点对象?衬里?多边形?

I'm trying to insert geometry objects into my supabase postgres table.

I have the following geometry columns:

Column nameData type
pointPOINT
line_stringLINESTRING
polygonPOLYGON

I have tried multiple ways to insert POINT objects (I haven't gotten to the linestrings or polygons, seeing that they are more complicated).


const geometry = { x: -123, y: -123 }

const point = `(${geometry.x}, ${geometry.y})`; // Try number 1
const point = `point(${geometry.x},${geometry.y})`; // Try number 2
const point = `ST_SetSRID(ST_MakePoint(${geometry.y}, ${geometry.x}), 4326)`; // Try number 3

const { data, error } = await supabase.from('geometries').upsert({ point });

All three gave the same error:

{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"(0" <-- parse error at position 2 within geometry'
}
{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"point(0, 0" <-- parse error at position 10 within geometry'
}
{
  message: 'parse error - invalid geometry',
  code: 'XX000',
  details: null,
  hint: '"ST" <-- parse error at position 2 within geometry'
}

How do I insert/upsert point objects? Linestrings? Polygons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2025-01-31 04:28:12

对于,您必须删除坐标值之间的逗号。

POINT(${geometry.x} ${geometry.y})

让我知道这是怎么回事!

For point, you have to remove the comma between the coordinates values.

POINT(${geometry.x} ${geometry.y})

Let me know how it goes with this!

心是晴朗的。 2025-01-31 04:28:12

如书面在这里

众所周知的二进制(WKB)提供了一种便携式,全精度表示空间数据作为二进制数据(字节的数组)

关于精确度的二进制数据(字节数组)的便携式,完整的表述,我们可以读取在这里

WKB格式对于从数据库中读取几何数据并保持完整的数字精度很有用。这避免了文本格式可能发生的精确四舍五入,例如wkt。

几何可以用wkb使用openlayers转换。这样,我们就不需要在查询中使用PostGIS功能。基于 documentation> WKB。

import WKB from 'ol/format/WKB';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';

const pointCoordinates = [...,...];

const feature = new Feature({
    geometry: new Point(pointCoordinates),
});

const format = new WKB();

const geomWKB = format.writeFeature(feature);

As written here:

Well-Known Binary (WKB) provides a portable, full-precision representation of spatial data as binary data (arrays of bytes)

Concerning precision we can read here:

WKB format is useful to read geometry data from the database and maintaining full numeric precision. This avoids the precision rounding that can happen with text formats such as WKT.

Geometries can be transformed in WKB with OpenLayers. This way we don't need to use PostGIS functions in our query. Based on the documentation I managed to update/insert geometries as WKB.

import WKB from 'ol/format/WKB';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';

const pointCoordinates = [...,...];

const feature = new Feature({
    geometry: new Point(pointCoordinates),
});

const format = new WKB();

const geomWKB = format.writeFeature(feature);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文