Javascript pg承诺将多多边形错误插入postgis数据库
因此,我使用pg promise将多个geojson多多边形插入到postgis数据库中。插入到数据库中工作正常,但对于数据库中的某些行,我得到了一个奇怪的行为,即单元格中填充了两行。第一行是加载消息,第二行是实际的geom对象,更奇怪的是,它从geojson直接转换为postgis geomJavascript pg承诺将多多边形错误插入postgis数据库,javascript,postgresql,gis,postgis,pg-promise,Javascript,Postgresql,Gis,Postgis,Pg Promise,因此,我使用pg promise将多个geojson多多边形插入到postgis数据库中。插入到数据库中工作正常,但对于数据库中的某些行,我得到了一个奇怪的行为,即单元格中填充了两行。第一行是加载消息,第二行是实际的geom对象,更奇怪的是,它从geojson直接转换为postgis geom function createBorder(pathToJSON, table) { fs.readFile(pathToJSON, {encoding: 'UTF-8'}, (err
function createBorder(pathToJSON, table) {
fs.readFile(pathToJSON,
{encoding: 'UTF-8'},
(err, data) => {
let geoJSON = JSON.parse(data);
geoJSON.features.forEach(f => {
f.geometry.crs = {
type: 'name',
properties: {
name: 'EPSG:4326'
}
}
db.none('INSERT INTO nyc_borders(geom)\
VALUES (ST_GeomFromGeoJSON(${geoJSON}))', {
geoJSON: f.geometry
})
.then((d) => {
console.log(f.geometry);
})
.catch(error => {
console.log("ERROR: ", error);
})
});
});
}
createBorder('./data/community_districts.geojson');
我简化了geoJSON输出,它基本上是从纽约市社区区边界下载的
Geojson:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"shape_leng": "51549.5578986",
"boro_cd": "311",
"shape_area": "103177785.347"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-73.97348373564797,
40.61137106069874
],
[
-73.97303089190211,
40.6090051063008
],
[
-73.97299433938896,
40.60881414180224
]
]
]
]
}
},
{
"type": "Feature",
"properties": {
"shape_leng": "65821.875617",
"boro_cd": "313",
"shape_area": "88195686.2688"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-73.96720294103956,
40.573326317397424
],
[
-73.96738975478877,
40.573258999904446
],
[
-73.9674356779313,
40.57320896967127
],
[
-73.96736390080571,
40.57304456895217
],
[
-73.98372152615246,
40.59582107821707
]
]
]
]
}
}
]
}
我的数据库中的一些图片:
所以我真的被卡住了,因为我不知道如何开始调试,因为插入确实起作用,而且geojson对象的转换看起来很好。实际上,我无法找出是谁导致了这种错误行为。我找到了解决问题的方法。图片中显示的两行让我困惑,其中只有datagrip添加的信息告诉我,巨大的多边形没有完全加载 我使用psql查看了相同的行:
SELECT ST_ASGEOJSON(geom) FROM <tablename> WHERE id=<myid>
从其中选择ST_ASGEOJSON(geom)id=
第二行就不会出现了。
然后我意识到这只是附加信息。通过使用,您可以完全控制数据的格式 例如,如果您有一个
数组[][2]
(如图所示的点),则可以按如下方式转换它们:
const toGeometry = g => ({ /* g = array[][2] (points) */
rawType: true,
toPostgres: a => {
const points = a.map(p => pgp.as.format('$1 $2', p));
return 'ST_GeomFromText(\'LINESTRING(' + points.join() + ')\')';
}
});
然后您可以将传递到几何(f.geometry)
以应用自定义格式
另见: