#!/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的更多相关文章

  1. Python:Shapefile矢量转化为GeoJSON格式

    在最近的项目中,完成了许多python处理矢量数据的算法程序,比如缓冲区分析.叠置分析.统计分析等,主要用到的是GDAL/OGR库,很多功能都参照了此链接中的示例:http://pcjericks.g ...

  2. 用shp制作geoJson格式地图数据(shp convert to geoJson)

    本文紧接前文,简单说明利用shp数据制作Echarts支持的geoJson格式的地图数据.本文以北京市通州区各镇的shp数据为例进行说明. 软件环境: ArcGIS 10.2 (ArcGIS 10.2 ...

  3. [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...

  4. OpenLayers 3 入门教程

    OpenLayers 3 入门教程摘要OpenLayers 3对OpenLayers网络地图库进行了根本的重新设计.版本2虽然被广泛使用,但从JavaScript开发的早期发展阶段开始,已日益现实出它 ...

  5. 【GIS】postgres(postgis) --》nodejs+express --》geojson --》leaflet

    一.基本架构 1.数据存储层:PostgreSQL-9.2.13 + postgis_2_0_pg92 2.业务处理层:Nodejs + Express + PG驱动 3.前端展示层:Leaflet ...

  6. PostgreSQL+PostGIS的使用 函数清单

    一. PostgreSQL与PostGIS的关系 PostgreSQL 是世界上技术最先进的开源数据库,其前身是1977年一个源于Berkeley名为Ingres的非关系型数据库,其项目领导人为Mic ...

  7. geotrellis使用(三十)使用geotrellis读取PostGIS空间数据

    前言 最近事情很多,各种你想不到的事情--such as singing and dancing--再加上最近又研究docker上瘾,所以geotrellis看上去似乎没有关注,其实我一直在脑中思考着 ...

  8. postgis几何操作函数集

    管理操作函数 AddGeometryColumn - Adds a geometry column to an existing table of attributes. By default use ...

  9. PostGis常用函数中文介绍

    记录常用PostGis常用函数: 1.OGC标准函数 管理函数: 添加几何字段 AddGeometryColumn(, , , , , ) 删除几何字段 DropGeometryColumn(, , ...

随机推荐

  1. ibatis轻松入门

    近日,由于公司项目应用开发的逻辑层使用的是iBatis.上网查了些资料,自己写了点demo入门.感觉良好.iBatis实在是比Hibernate很容易入门,贡献出来与各路菜鸟分享(后文附源码),希望得 ...

  2. WPF The Hard Way

    WPF The Hard Way Windows Presentation Foundation (WPF) 是微软下一代显示系统,用于生成能带给用户震撼视觉体验的 Windows 客户端应用程序.  ...

  3. java加密解密的学习

    注:此文章只是对如何学习java加密解密技术做一个讲解.并不涉及具体的知识介绍,如果有需要请留言,有时间我补冲长.个人觉着学习一个学习方法比学习一个知识点更有价值的多. 首先,对于加密解密知识体系没有 ...

  4. C#抓取天气数据

    使用C#写的一个抓取天气数据的小工具,使用正则匹配的方式实现,代码水平有限,供有需要的同学参考.压缩包中的两个sql语句是建表用的. http://files.cnblogs.com/files/yu ...

  5. [译]使用scikit-learn进行机器学习(scikit-learn教程1)

    原文地址:http://scikit-learn.org/stable/tutorial/basic/tutorial.html 翻译:Tacey Wong 概要: 该章节,我们将介绍贯穿scikit ...

  6. JavaScript基础—插曲02

    Js学习基础 1:关于js中的数组 这里面可以是键值对的形式来存储的,我们必须利用forIn来输出,这就相当于foreach一样. var dir = new Array(); dir['0'] = ...

  7. 浅谈Jquery中的bind(),live(),delegate(),on()绑定事件方式

    前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅,也希望该文章日后能 ...

  8. C#控件及常用设计整

    C#控件及常用设计整 1.窗体    1 2.Label 控件    3 3.TextBox 控件    4 4.RichTextBox控件    5 5.NumericUpDown 控件    7 ...

  9. ASP.NET MVC使用SSI来实现页面静态化

    页面静态化分为两种:伪静态和真静态,这里主要介绍的是真静态. 进入正题之前先简单介绍一下SSI和shtml: 1).SSI是Server Side Include的简称(服务器端嵌入) 2).shtm ...

  10. 15天玩转redis —— 第八篇 你不得不会的事务玩法

    我们都知道redis追求的是简单,快速,高效,在这种情况下也就拒绝了支持window平台,学sqlserver的时候,我们知道事务还算是个比较复杂的东西, 所以这吊毛要是照搬到redis中去,理所当然 ...