ogr2ogr导入导出数据。

1.导入shp数据

ogr2ogr -f "PostgreSQL"  -sql "SELECT  ISO2, NAME FROM TM_WORLD_BORDERS_3 WHERE REGION=2 " -nlt MULTIPOLYGON   PG:"host=localhost port=5432 dbname=postgis_32_sample user=postgres password=postgres schemas=public"  -nln africa_countries -lco GEOMETRY_NAME=the_geom TM_WORLD_BORDERS_3.shp

其中,参数的含义如下:

参数含义
-f 表示输出文件类型。
-sql表示要执行的sql语句。
 -nln图层的名称
 -nlt图层定义的几何类型

2.查找日照时间最长的100个地区。

select ST_AsText(the_geom) AS the_geom, bright_t31 from global_24h 
order by bright_t31 desc limit 100;

在pgAdmin中的查询结果如下:

3.查找非洲日照时间最长的100个地区。

select ST_AsText(f.the_geom) AS the_geom, f.bright_t31, ac.iso2, ac.name 
from global_24h as f
join africa_countries as ac
on ST_Contains(ac.the_geom, ST_Transform(f.the_geom,4326))
order by f.bright_t31 desc limit 100;

在pgAdmin中的查询结果如下:

4.将查询结果以GeoJson的格式导出

ogr2ogr -f  "GeoJSON"  -t_srs EPSG:4326 warmest_hs.geojson  -sql "select f.the_geom AS the_geom, f.bright_t31, ac.iso2, ac.name from global_24h as f  join africa_countries as ac on ST_Contains(ac.the_geom, ST_Transform(f.the_geom,4326)) order by f.bright_t31 desc limit 100"  PG:"host=localhost port=5432 dbname=postgis_32_sample user=postgres password=postgres schemas=public" 

其中,参数-t_srs表示 在输出上重新投影或转换为 EPSG:4326 坐标系下。

5.在QGIS中,查看africa_countries 和 warmest_hs.geojson 信息

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将OGR格式的矢量数据导出为JSON格式,你可以使用`ogr`库中的`ogr2ogr`命令行工具或使用Python中的`ogr`模块来实现。以下是使用`ogr`模块的示例代码: ```python from osgeo import ogr # 输入和输出文件路径 input_file = 'your_input_file.shp' output_file = 'your_output_file.json' # 打开输入数据源 input_ds = ogr.Open(input_file) input_layer = input_ds.GetLayer() # 创建输出数据源 output_driver = ogr.GetDriverByName('GeoJSON') output_ds = output_driver.CreateDataSource(output_file) output_layer = output_ds.CreateLayer('', None, ogr.wkbPolygon) # 复制输入图层的字段定义到输出图层 input_layer_defn = input_layer.GetLayerDefn() for i in range(input_layer_defn.GetFieldCount()): field_defn = input_layer_defn.GetFieldDefn(i) output_layer.CreateField(field_defn) # 复制输入图层的要素到输出图层 for feature in input_layer: output_layer.CreateFeature(feature) # 关闭数据源 input_ds = None output_ds = None ``` 在这个示例中,我们首先指定输入文件路径和输出文件路径。然后,我们使用`ogr`库打开输入数据源,并获取输入图层。 接下来,我们创建输出数据源,并指定输出驱动为GeoJSON。然后,我们创建输出图层,并复制输入图层的字段定义到输出图层。 然后,我们使用一个循环来遍历输入图层中的要素,并将它们复制到输出图层中。 最后,我们关闭输入和输出数据源,完成导出过程。 请确保你已经安装了GDAL库,并将代码中的`your_input_file.shp`替换为你自己的输入矢量数据文件路径,将`your_output_file.json`替换为你想要保存的输出JSON文件路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值