如何从数据库动态创建geojson文件

时间:2019-12-13 10:07:19

标签: java hibernate spring-boot jpa

我正在尝试创建从数据库中提取数据的geojson文件。

当创建geojson时,解析为inputStream,将其存储在具有fileManager的路径中。

我想从数据库中加载这个方案。特征包含一个特征数组,每个特征包含一个id,具有值的属性对象以及一个包含数组的几何字段。

这是应该如何从数据库中加载的方法:(方案1)

{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "id": "AFG",
        "properties": {
            "name": "Afghanistan"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [...]
        }
    }]
}

我有两个问题:

  • 当我向邮递员发出请求时,请向我显示我的geojson方案,如下所示:(方案2)

    [{
        "geometry": {
            "coordinates": [...],
            "type": "Polygon"
        },
        "type": "Feature",
        "table_id": 1,
        "properties": {
            "name": "Afghanistan"
        }
    }]
    

下载文件时,使用fileManager将其存储在folderPath上,如下所示:(方案3)

[{
    geometry = {
        coordinates = []
        type = Polygon
    },
    type = Feature,
    table_id = 1,
    properties = {
        name = Afghanistan
    }
}]

所以我的问题是:

  • 如何从数据库中加载每个功能并像方案1一样显示?
  • 为什么在存储文件时将:替换为=
  • 如果使用大型文件,如何创建文件而不占用大量内存?

谢谢

这是我的代码:

public List<Object> getGeoJsonFromTable(String nameTable) {
    String SQL = "SELECT table_id, CAST(properties AS text) as properties, ST_AsGeoJSON(geom) as geometry FROM " + nameTable + " ORDER BY table_id ASC;";               
    List<Map<String, Object>> result = jdbcTemplate.queryForList(SQL);

    JSONObject geojson = new JSONObject();
    geojson.put("type", "FeatureCollection");

    JSONArray json = new JSONArray();
    for (int i=0; i<result.size(); i++) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "Feature");
        jsonObject.put("table_id", result.get(i).get("table_id") );
        jsonObject.put("geometry",  serializeProperty( result.get(i).get("geometry").toString() ) );
        jsonObject.put("properties", serializeProperty( result.get(i).get("properties").toString() ) );
        json.put(jsonObject);
    }
    geojson.put("feautures", json);
    List<Object> gson = json.toList();

    String fileName = nameTable + "_" + String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()) + ".geojson";
    Path folderPath = Paths.get(ValidationUtil.initializeField(null, ""));
    String fileNewPath = folderPath.resolve(fileName).toString();
    try {
        fileManager.getFileStore().saveFile(new ByteArrayInputStream( gson.toString().getBytes("UTF-8") ), fileNewPath);
    } catch (UnsupportedEncodingException e) {
        log.error("ERROR: " + e.getMessage().toString());
        e.printStackTrace();
    }

    return gson;
}

解决方案:

我已经解决了如何从数据库创建geojson文件的问题,但是我想通过更好的实现来实现。如何实现更好的实现?

public String getGeoJsonFromTable(String nameTable) {
    String SQL = "SELECT table_id, CAST(properties AS text) as properties, ST_AsGeoJSON(geom) as geometry FROM " + nameTable + " ORDER BY table_id ASC;";               
    List<Map<String, Object>> result = jdbcTemplate.queryForList(SQL);

    JSONObject geojson = new JSONObject();
    geojson.put("type", "FeatureCollection");

    JSONArray json = new JSONArray();
    for (int i=0; i<result.size(); i++) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "Feature");
        jsonObject.put("id", result.get(i).get("table_id") );
        jsonObject.put("geometry",  serializeProperty( result.get(i).get("geometry").toString() ) );
        jsonObject.put("properties", serializeProperty( result.get(i).get("properties").toString() ) );
        json.put(jsonObject);
    }
    geojson.put("feautures", json);

    String fileName = nameTable + "_" + String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()) + ".geojson";
    Path folderPath = Paths.get(ValidationUtil.initializeField(null, ""));
    String fileNewPath = folderPath.resolve(fileName).toString();
    try {
        fileManager.getFileStore().saveFile(new ByteArrayInputStream( geojson.toString().getBytes("UTF-8") ), fileNewPath);
    } catch (UnsupportedEncodingException e) {
        log.error("ERROR: " + e.getMessage().toString());
        e.printStackTrace();
    }
    return geojson.toString();
}

0 个答案:

没有答案