将PostGIS转化为GeoJSON
#!/usr/bin/env python
# -*- coding: utf-8 -*- import psycopg2
import json
from geojson import loads, Feature, FeatureCollection # Database Connection Info
db_host = "localhost"
db_user = "pluto"
db_passwd = "stars"
db_database = "py_geoan_cb"
db_port = "" # connect to DB
conn = psycopg2.connect(host=db_host, user=db_user,
port=db_port, password=db_passwd, database=db_database) # create a cursor
cur = conn.cursor() # the PostGIS buffer query
buffer_query = """SELECT ST_AsGeoJSON(ST_Transform(
ST_Buffer(wkb_geometry, 100,'quad_segs=8'),4326))
AS geom, name
FROM geodata.schools""" # execute the query
cur.execute(buffer_query) # return all the rows, we expect more than one
dbRows = cur.fetchall() # an empty list to hold each feature of our feature collection
new_geom_collection = [] # loop through each row in result query set and add to my feature collection
# assign name field to the GeoJSON properties
for each_poly in dbRows:
geom = each_poly[0]
name = each_poly[1]
geoj_geom = loads(geom)
myfeat = Feature(geometry=geoj_geom, properties={'name': name})
new_geom_collection.append(myfeat) # use the geojson module to create the final Feature Collection of features created from for loop above
my_geojson = FeatureCollection(new_geom_collection) # define the output folder and GeoJSon file name
output_geojson_buf = "../geodata/out_buff_100m.geojson" # save geojson to a file in our geodata folder
def write_geojson():
fo = open(output_geojson_buf, "w")
fo.write(json.dumps(my_geojson))
fo.close() # run the write function to actually create the GeoJSON file
write_geojson() # close cursor
cur.close() # close connection
conn.close()
将PostGIS转化为GeoJSON的更多相关文章
- Python:Shapefile矢量转化为GeoJSON格式
在最近的项目中,完成了许多python处理矢量数据的算法程序,比如缓冲区分析.叠置分析.统计分析等,主要用到的是GDAL/OGR库,很多功能都参照了此链接中的示例:http://pcjericks.g ...
- 用shp制作geoJson格式地图数据(shp convert to geoJson)
本文紧接前文,简单说明利用shp数据制作Echarts支持的geoJson格式的地图数据.本文以北京市通州区各镇的shp数据为例进行说明. 软件环境: ArcGIS 10.2 (ArcGIS 10.2 ...
- [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度
csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...
- OpenLayers 3 入门教程
OpenLayers 3 入门教程摘要OpenLayers 3对OpenLayers网络地图库进行了根本的重新设计.版本2虽然被广泛使用,但从JavaScript开发的早期发展阶段开始,已日益现实出它 ...
- 【GIS】postgres(postgis) --》nodejs+express --》geojson --》leaflet
一.基本架构 1.数据存储层:PostgreSQL-9.2.13 + postgis_2_0_pg92 2.业务处理层:Nodejs + Express + PG驱动 3.前端展示层:Leaflet ...
- PostgreSQL+PostGIS的使用 函数清单
一. PostgreSQL与PostGIS的关系 PostgreSQL 是世界上技术最先进的开源数据库,其前身是1977年一个源于Berkeley名为Ingres的非关系型数据库,其项目领导人为Mic ...
- geotrellis使用(三十)使用geotrellis读取PostGIS空间数据
前言 最近事情很多,各种你想不到的事情--such as singing and dancing--再加上最近又研究docker上瘾,所以geotrellis看上去似乎没有关注,其实我一直在脑中思考着 ...
- postgis几何操作函数集
管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...
- PostGis常用函数中文介绍
记录常用PostGis常用函数: 1.OGC标准函数 管理函数: 添加几何字段 AddGeometryColumn(, , , , , ) 删除几何字段 DropGeometryColumn(, , ...
随机推荐
- Debugging WebLogic Server Applications Using Eclipse and the WebLogic-Plugin
http://www.oracle.com/technetwork/cn/tutorials/eclipse-plugin-093411.html
- Android之Notification介绍
Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...
- Windows Azure Cloud Service (10) Role的生命周期
<Windows Azure Platform 系列文章目录> 在上一章内容中,我们提到了Windows Azure会依次调用角色(Role)实例的OnStart()方法和Run()方法. ...
- ASP.NET 文件下载
using System; using System.Web; using System.IO; public partial class _Default : System.Web.UI.Page ...
- libqxt编译
一.说明 编译环境:win10.qt5.6.1-1.vs2013和libqxt源码(从git上下载) libqxt:libqxt 关于libqxt的说明,请到libqxt的官网阅读,说着看图1,图1是 ...
- SQL Server时间粒度系列----第6节基于当前日的小时数和分钟数与mysql unix_timestamp和from_unixtime的mssql实现
本文目录列表: 1.基于当前日的小时数和分钟数2.mysql unix_timestamp和from_unixtime的mssql实现 3.总结语 4.参考清单列表 基于当前日的小时数和分钟数 ...
- Python性能提升小技巧
第一部分 1-使用内建函数: 你可以用Python写出高效的代码,但很难击败内建函数. 经查证. 他们非常快速 2-使用 join() 连接字符串. 你可以使用 + 来连接字符串. 但由于string ...
- 使用Python对Excel表格进行简单的读写操作(xlrd/xlwt)
算是一个小技巧吧,只是进行一些简单的读写操作.让人不爽的是xlrd和xlwt是相对独立的,两个模块的对象不能通用,读写无法连贯操作,只能单独读.单独写,尚不知道如何解决. #①xlrd(读) #cod ...
- 30天C#基础巩固----程序集,反射
一:认识程序集 只要是使用VS就会和程序集打交道,我们通过编辑和生产可执行程序就会自动生成程序集.那么什么事程序集呢,.net中的dll与exe文件的都是程序集(Assembly). ...
- LeetCode - 52. N-Queens II
52. N-Queens II Problem's Link --------------------------------------------------------------------- ...