1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import psycopg2
  5. import json
  6. from geojson import loads, Feature, FeatureCollection
  7.  
  8. # Database Connection Info
  9. db_host = "localhost"
  10. db_user = "pluto"
  11. db_passwd = "stars"
  12. db_database = "py_geoan_cb"
  13. db_port = ""
  14.  
  15. # connect to DB
  16. conn = psycopg2.connect(host=db_host, user=db_user,
  17. port=db_port, password=db_passwd, database=db_database)
  18.  
  19. # create a cursor
  20. cur = conn.cursor()
  21.  
  22. # the PostGIS buffer query
  23. buffer_query = """SELECT ST_AsGeoJSON(ST_Transform(
  24. ST_Buffer(wkb_geometry, 100,'quad_segs=8'),4326))
  25. AS geom, name
  26. FROM geodata.schools"""
  27.  
  28. # execute the query
  29. cur.execute(buffer_query)
  30.  
  31. # return all the rows, we expect more than one
  32. dbRows = cur.fetchall()
  33.  
  34. # an empty list to hold each feature of our feature collection
  35. new_geom_collection = []
  36.  
  37. # loop through each row in result query set and add to my feature collection
  38. # assign name field to the GeoJSON properties
  39. for each_poly in dbRows:
  40. geom = each_poly[0]
  41. name = each_poly[1]
  42. geoj_geom = loads(geom)
  43. myfeat = Feature(geometry=geoj_geom, properties={'name': name})
  44. new_geom_collection.append(myfeat)
  45.  
  46. # use the geojson module to create the final Feature Collection of features created from for loop above
  47. my_geojson = FeatureCollection(new_geom_collection)
  48.  
  49. # define the output folder and GeoJSon file name
  50. output_geojson_buf = "../geodata/out_buff_100m.geojson"
  51.  
  52. # save geojson to a file in our geodata folder
  53. def write_geojson():
  54. fo = open(output_geojson_buf, "w")
  55. fo.write(json.dumps(my_geojson))
  56. fo.close()
  57.  
  58. # run the write function to actually create the GeoJSON file
  59. write_geojson()
  60.  
  61. # close cursor
  62. cur.close()
  63.  
  64. # close connection
  65. 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. struts深入理解之登录示例的源码跟踪

    废话不多,直接上图:(色泽比较重的是追踪的路径)

  2. Network - SNMP

    Simple Network Management ProtocolWikipediahttps://en.wikipedia.org/wiki/Simple_Network_Management_P ...

  3. 在SQL Server里为什么我们需要更新锁

    今天我想讲解一个特别的问题,在我每次讲解SQL Server里的锁和阻塞(Locking & Blocking)都会碰到的问题:在SQL Server里,为什么我们需要更新锁?在我们讲解具体需 ...

  4. 将RichTextBox的内容直接写入数据库:

    将RichTextBox的内容直接写入数据库: private void button1_Click(object sender, EventArgs e) {    System.IO.Memory ...

  5. 使用ajax和js无刷新改变页面内容和地址栏URL

    发现一个可以改变地址栏,而不导致页面刷新的东东. Chrome, FF测试通过,不支持IE. 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与页面展现内容一致(符合人们对传统网页的 ...

  6. 关于ExtJs4的Grid带 查询 参数 分页(baseParams-->extraParams)

    (园里很多文章,美名其曰 :ExtJs GridPanel+查询条件+分页.  但是加查询条件后点击下一页,查询条件失效,求你们自己测试明白再显摆 不要误导我这种新人.) ExtJs6发布了,ExtJ ...

  7. 一个故事讲清楚NIO

    转载请引用:一个故事讲清楚NIO 假设某银行只有10个职员.该银行的业务流程分为以下4个步骤: 1) 顾客填申请表(5分钟): 2) 职员审核(1分钟): 3) 职员叫保安去金库取钱(3分钟): 4) ...

  8. WPFINotifyPropertyChanged使用技巧

    WPFINotifyPropertyChanged使用技巧 public class NotifyPropertyChanged : INotifyPropertyChanged { public e ...

  9. Ado.net 三[SQL注入,DataAdapter,sqlParameter,DataSet]

    1.SQL注入:SQL注入攻击是web应用程序的一种安全漏洞,可以将不安全的数据提交给运用程序,使应用程序在服务器上执行不安全的sql命令.使用该攻击可以轻松的登录运用程序. 例如:该管理员账号密码为 ...

  10. 【Java每日一题】20161201

    20161130问题解析请点击今日问题下方的"[Java每日一题]20161201"查看 package Dec2016; public class Ques1201 { publ ...