java geojson_Java读写geojson

/** 读 **/

public Map getSiteMap() {

Map map = new HashMap();

URL dataUrl = this.getClass().getClassLoader().getResource("public\\station.json"); // 259

try{

BufferedReader br =new BufferedReader(new FileReader(new File(dataUrl.toURI())));

String s = null;

while((s = br.readLine()) != null){ // s 为原生的json串

// System.out.println("00=="+s);

JSONObject jo = new JSONObject(s); // 创建一个包含原始json串的json对象

JSONArray features = jo.getJSONArray("features"); //找到features的json数组

for (int i = 0; i < features.length(); i++) {

SiteEntity siteEntity = new SiteEntity();

JSONObject info = features.getJSONObject(i); // 获得features的第i个对象

JSONObject geometry = info.getJSONObject("geometry");

JSONObject properties =

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用GeoTools库将GeoJSON数据导入到数据库中。下面是一个简单的步骤: 1. 首先,确保你已经将GeoTools库添加到你的Java项目中。 2. 导入所需的类: ```java import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.Transaction; import org.geotools.data.collection.CollectionFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.geojson.feature.FeatureJSON; import org.opengis.feature.simple.SimpleFeature; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; ``` 3. 创建一个方法来导入GeoJSON数据到数据库中: ```java public void importGeoJsonToDatabase(String geoJsonFilePath, String databaseTypeName, String databaseUrl, String tableName) { try { // 读取GeoJSON文件 File geoJsonFile = new File(geoJsonFilePath); FileInputStream inputStream = new FileInputStream(geoJsonFile); // 创建FeatureJSON对象 FeatureJSON featureJSON = new FeatureJSON(); // 将GeoJSON文件解析为SimpleFeature对象 SimpleFeature feature = (SimpleFeature) featureJSON.readFeature(inputStream); // 创建数据源 Map<String, Object> params = new HashMap<>(); params.put("dbtype", databaseTypeName); params.put("host", databaseUrl); DataStore dataStore = DataStoreFinder.getDataStore(params); // 获取要导入数据的目标表 SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(tableName); // 开始事务 Transaction transaction = new DefaultTransaction(); featureStore.setTransaction(transaction); try { // 导入数据 featureStore.addFeatures(DataUtilities.collection(feature)); // 提交事务 transaction.commit(); } catch (Exception e) { // 回滚事务 transaction.rollback(); e.printStackTrace(); } finally { // 关闭数据源 transaction.close(); dataStore.dispose(); } } catch (Exception e) { e.printStackTrace(); } } ``` 4. 调用方法进行导入: ```java importGeoJsonToDatabase("path/to/geojson/file.json", "postgis", "localhost:5432/database", "tableName"); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值