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

Java 如何从数据库动态创建geojson文件,java,hibernate,spring-boot,jpa,Java,Hibernate,Spring Boot,Jpa,我试图创建一个geojson文件,从数据库中提取数据 创建geojson时,解析inputStream以将其存储在具有fileManager的路径中 我想从数据库加载一个有这个方案。要素包含要素数组,每个要素包含一个id、带值的属性对象和一个包含数组的几何体字段 这是如何从数据库中加载:(方案1) 我有两个问题: 当我从邮递员那里发出请求时,向我展示我的geojson方案如下:(方案2) 下载文件时,使用fileManager将其存储在folderPath上,如下所示:(方案3) 所以我的

我试图创建一个geojson文件,从数据库中提取数据

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

我想从数据库加载一个有这个方案。要素包含要素数组,每个要素包含一个id、带值的属性对象和一个包含数组的几何体字段

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

我有两个问题:

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

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

所以我的问题是:

  • 如何从数据库加载每个特性并显示如方案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;
}
公共列表getGeoJsonFromTable(字符串名称表){
String SQL=“选择表格id,将属性转换为文本,将ST_AsGeoJSON(geom)作为几何体,从“+nameTable+”按表格id ASC;排序”;
列表结果=jdbcTemplate.queryForList(SQL);
JSONObject geojson=新的JSONObject();
geojson.put(“类型”、“特征集合”);
JSONArray json=新的JSONArray();
对于(int i=0;i
[{
    "geometry": {
        "coordinates": [...],
        "type": "Polygon"
    },
    "type": "Feature",
    "table_id": 1,
    "properties": {
        "name": "Afghanistan"
    }
}]
[{
    geometry = {
        coordinates = []
        type = Polygon
    },
    type = Feature,
    table_id = 1,
    properties = {
        name = Afghanistan
    }
}]
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;
}
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();
}