我有一个nodeJS LeafletJS GIS应用程序,该应用程序在地图上显示了多个多边形,并带有PostgreSQL 9.5数据库。我试图创建一个函数,以按项目ID搜索和返回多边形列表。如果单个PostgreSQL数据库行与项目ID匹配,则该行会以JSON的形式正确返回,但是会为多行返回错误。 阅读其他类似的问题,我可能会认为需要“ LOOP”或“ NEXT”。但是,同一应用程序中的另一个函数(从中派生该函数)会根据多边形是否在用户的可见地图边界框内而成功返回许多行。
搜索基于“ pid”,主键(自动递增)为“ id”。
下面是问题函数:
export class MapasPage {
map: any;
markers:any;
//datos
contacts: ContactList[];
Latitud:any;
Longitud:any;
Lat1:any;
Lon1:any;
Lat2:any;
Lon2:any;
constructor(public navCtrl: NavController, public geolocation: Geolocation, public platform:Platform,private contactProvider: ContactProvider) {
///datos
this.contactProvider.getAll()
.then((result) => {
this.contacts = result;
let answer = this.contacts.map(x=> {
let {name2, name3} = x.contact;
let obj = {name2,name3}
return obj
})
this.Latitud= answer.map(({ name2}) => name2);
this.Longitud= answer.map(({ name3}) => name3);
// console.log(answer)
this.Lat1=this.Latitud[0];
this.Lat2=this.Latitud[1];
this.Lon1=this.Longitud[0];
this.Lon2=this.Longitud[1];
// console.log(this.Lat1)
})
}
下面是它的工作函数形式:
CREATE OR REPLACE FUNCTION data.polygons_projectID(
IN projectID int,
OUT plg json
)
RETURNS json AS
$BODY$
BEGIN
SELECT row_to_json(fc.*) as polygons into plg
FROM
(
SELECT 'FeatureCollection' AS type,
array_to_json(array_agg(f.*)) AS features
FROM
(
SELECT 'Feature' AS type,
(
SELECT row_to_json(t) as properties
FROM
(
SELECT * FROM data.polygons WHERE pid =$1
) t
),
ST_AsGeoJSON(geom,7)::json as geometry
FROM data.polygons dp WHERE pid = $1
) f
) fc;
return;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;