我正在嘗試使用Leaflet和CartoDB創建一個Web應用程序。我正在嘗試使用this tutorial來讀取和寫入公共CartoDB表中的數據。如何使用Leaflet.draw和CartoDB更新/插入表格
本教程解釋瞭如何使用安全性定義器來完成此操作。無論出於何種原因,我在應用程序中使用leaflet.draw創建的功能都沒有插入到我的表中。我仍然對編碼不熟悉,並且不確定在我的代碼中如何實際調用SQL查詢,但我已經嘗試複製示例源代碼並且沒有任何執行的運氣。
目標是允許用戶在地圖上創建和編輯它們的點並將這些點保存到CartoDB中的衆包數據庫中。
爲我的項目的代碼可以在這裏看到:https://github.com/zrobby/crowdsource-storymap
說我有如下所示執行難度和來自上面鏈接的教程中的特定SQL查詢。
DROP FUNCTION IF EXISTS leaflet_upsert_usercomments(int[], text[]);
-- Returns a set of op,cartodb_id values where op means:
-- deleted: -1
-- updated: 0
-- inserted: 1
CREATE OR REPLACE FUNCTION leaflet_upsert_usercomments(
cartodb_ids integer[],
geojsons text[])
RETURNS TABLE(op int, cartodb_id int)
LANGUAGE plpgsql SECURITY DEFINER
RETURNS NULL ON NULL INPUT
AS $$
DECLARE
sql text;
BEGIN
sql := 'WITH n(cartodb_id,the_geom) AS (VALUES ';
-- Iterate over the values
FOR i in 1 .. array_upper(geojsons, 1)
LOOP
IF i > 1 THEN sql := sql || ','; END IF;
sql :=sql || '('||cartodb_ids[i]||','
|| 'ST_SetSRID(ST_GeomFromGeoJSON(NULLIF('''|| geojsons[i] ||''','''')),4326))';
END LOOP;
sql := sql || '), do_update AS ('
|| 'UPDATE leaflet_data p '
|| 'SET the_geom=n.the_geom FROM n WHERE p.cartodb_id = n.cartodb_id '
|| 'AND n.the_geom IS NOT NULL '
|| 'RETURNING p.cartodb_id), do_delete AS ('
|| 'DELETE FROM leaflet_data p WHERE p.cartodb_id IN ('
|| 'SELECT n.cartodb_id FROM n WHERE cartodb_id >= 0 AND '
|| ' n.the_geom IS NULL) RETURNING p.cartodb_id), do_insert AS ('
|| 'INSERT INTO leaflet_data (the_geom)'
|| 'SELECT n.the_geom FROM n WHERE n.cartodb_id < 0 AND '
|| ' n.the_geom IS NOT NULL RETURNING cartodb_id) '
|| 'SELECT 0,cartodb_id FROM do_update UNION ALL '
|| 'SELECT 1,cartodb_id FROM do_insert UNION ALL '
|| 'SELECT -1,cartodb_id FROM do_delete';
RAISE DEBUG '%', sql;
RETURN QUERY EXECUTE sql;
END;
$$;
-- Grant access to the public user
GRANT EXECUTE ON FUNCTION leaflet_upsert_usercomments(integer[],text[]) TO publicuser;
相關問題:http://gis.stackexchange.com/questions/134544/in-cartodb-best-way-to-have- a-table-with-multiple-contributors – zrobby
相關解決方案:https://github.com/radumas/crowdmap-basic – zrobby