mysql geojson_关于县级市的geojson数据格式的查找以及生成方式?

本文介绍了如何使用Python读取SHP文件并转换为GeoJSON格式,提供了一个自编的Python脚本示例,该脚本处理了POINT、LINE和POLYGON类型的数据。同时提到了MySQL 5.7中的ST_AsGeoJSON函数,并建议在PostGIS中进行转换以获取更完整的信息。
摘要由CSDN通过智能技术生成

这个你d3.js数据里面去找,下面这个网站去下载,

或者你可以下载SHP文件,参考这个人的

===========================

此外MySQL57 里面有 ST_AsGeoJSON

===========================分割线====================================

# this is a file to decode the shape file into the geojson

# a.shape file consists of a.shp ,a.prj,a.shx,a.dbf

# .shp file header struct

# ---------------------------

# 1.Big Integer 4 byte

# 2.Little Integer 4 byte

# 3.Little Double 8 byte

# ---------------------------

# typedef struct header{

# UNDEFINED 7 Big Integers 28byte

# Version 1 Little Integers 4byte

# ShapeType 1 Little Integers 4byte

# EXTENT 4 Little Double 32byte

# 3D-PROPS 4 Little Double 32byte

# }

#

#

import struct

import json

def readShp(filepath):

features = []

bbox=[]

featurecollections = {"type": "FeatureCollection", "features": features}

with open(filepath, 'rb') as f:

unused = unpack('>iiiiiii', f.read(4 * 7))

version, = unpack('

shapeType, = unpack('

xmin, ymin, xmax, ymax = unpack('

bbox=[xmin,ymin,xmax,ymax]

featurecollections["bbox"]=bbox

extensions = unpack('

# print(unused,"version=",version,shapeType,xmin,ymin,xmax,ymax)

# (9994, 0, 0, 0, 0, 0, 1114) version= (1000,) (1,) -4480198.5 1433525.79887 24615124.978985116 6502586.830347195

# header end here

#

# begin read records

# a record consist of header,content

# header

# ---------------

# serial Big Integer 4byte

# length Big Integer 4byte the length of the following record ,and units is 2 byte length*2-4(shapetype)

# shapetype Litte Integer 4byte

# POINT:[serial][length][shapetype][lat][lnt]

# Little Double 4yte Little Integer Little integer Little Integer * #lines Little Double 2*#vertexes

# LINE:[extent:xmin,ymin,xmax,ymax][#lines or #ploygons][ #vertexes ][#start index of each polyline or polygon][ vertexes list ]

#

# Polygon:like above

while True:

try:

# point

if shapeType == 1:

serial, length = unpack('>ii', f.read(4 + 4))

shpType, = unpack('

x, y = unpack('

print(x, y)

geos = {

'type': "Point",

"coordinates": [x, y]

}

feature = {

"type": "Feature",

"geometry": geos,

"properties": {}

}

# print(feature)

features.append(feature)

elif shapeType==3:

serial, length = unpack('>ii', f.read(4 + 4))

shpType, = unpack('

xmin, ymin, xmax, ymax = unpack('

FeatureBBOX=[xmin,ymin,xmax,ymax]

nums_of_lines, nums_of_vertexes = unpack('

start_index = [unpack('

vertexes = [list(unpack('

if nums_of_lines==1:

#convert to LineString

geos={

"type":"LineString",

"coordinates":vertexes

}

feature = {

"type": "Feature",

"bbox":FeatureBBOX,

"geometry": geos,

"properties": {}

}

else:

lines=[[] for i in range(0,nums_of_lines)]

for i in range(0,nums_of_lines-1):

lines[i]=vertexes[start_index[i]:start_index[i+1]]

lines[nums_of_lines-1]=vertexes[start_index[nums_of_lines-1]:]

geos={

"type":"MultiLineString",

"coordinates":lines

}

feature = {

"type": "Feature",

"geometry": geos,

"properties": {}

}

features.append(feature)

elif shapeType==5:

serial, length = unpack('>ii', f.read(4 + 4))

shpType, = unpack('

xmin, ymin, xmax, ymax = unpack('

nums_of_lines, nums_of_vertexes = unpack('

start_index = [unpack('

vertexes = [list(unpack('

if nums_of_lines==1:

#convert to LineString

#hi,why reverse,well that's shp record the point clockwise,but geojson new spec requires right-hand rule

#https://gis.stackexchange.com/questions/119150/order-of-polygon-vertices-in-general-gis-clockwise-or-counterclockwise

vertexes.reverse()

geos={

"type":"Polygon",

"coordinates":[vertexes]

}

feature = {

"type": "Feature",

"geometry": geos,

"properties": {}

}

else:

lines=[[] for i in range(0,nums_of_lines)]

for i in range(0,nums_of_lines-1):

lines[i]=vertexes[start_index[i]:start_index[i+1]]

#to follow the counter-clockwise rule or right-hand rule

lines[i].reverse()

lines[nums_of_lines-1]=vertexes[start_index[nums_of_lines-1]:]

geos={

"type":"MultiPolygon",

"coordinates":[lines]

}

feature = {

"type": "Feature",

"geometry": geos,

"properties": {}

}

features.append(feature)

except struct.error as e:

#there is a end of file,it's not so nice

break

f.close()

return featurecollections

if __name__ == "__main__":

filepath="Anhui.shp"

from struct import *

with open('test.json','w') as f:

f.write(json.dumps(readShp(filepath)))

——————————————————————————————

这是我写的一个python读shp文件转换成geojson文件的代码,没有第三方的。

直接可以下到县级别的,然后用这个python转换一下,基本都是polygon,缺点就是属性数据我没读,到底来说,还是在postgis中转最好,postgis用的是gdal的,所以安装gdal也可以的。

有时间再写读dbf的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值