spatiallite空间数据库转换到leaflet.js的GeoJson格式

来源:互联网 发布:网络教育笔试考试 编辑:程序博客网 时间:2024/05/19 19:57

        spatiallite是一个轻量型的空间数据库,可以将GIS的点线面数据以GeoJson的格式读取出来,但是这个GeoJson的格式与Leaflet的GeoJson格式不同,具体的格式大家可以去网上看。这里公布一下,转换的代码:


  public void queryGeometry(String tableName) {        net.sf.json.JSONObject jsonObject=new net.sf.json.JSONObject();        jsonObject.put("type","FeatureCollection");        List list = new ArrayList();        String query = "SELECT ROWID, AsGeoJSON(Geometry) from "+tableName+";";        try {            Stmt stmt = db.prepare(query);            while (stmt.step()) {                String rowID = stmt.column_string(0);                JSONObject geoJson=JSONObject.fromObject(stmt.column_string(1));                Map map = new HashMap();                map.put("ROWID",rowID);                map.put("tableName",tableName);                net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(map);                Map mapParent=new HashMap();                mapParent.put("type","Feature");                mapParent.put("geometry",geoJson);                mapParent.put("properties",json);                net.sf.json.JSONObject jsonParent = net.sf.json.JSONObject.fromObject(mapParent);                list.add(jsonParent);                Log.e(TAG,json.toString());                Log.e(TAG,jsonParent.toString());            }            jsonObject.put("features", JSONArray.fromObject(list));            Log.e(TAG,jsonObject.toString());            stmt.close();        }catch (Exception e){            Log.e(TAG,"查询数据库错误"+e.toString());        }    }

这里转换json使用了特定的jar包,可以去网上找。