概述:
一年一度的高考今天开始了,回想10年前,那是我第一次高考;10年后,作为一个GISER,在此给大家献上一个GISER的祝福,祝愿各位考生:考神附体,考完报考GIS专业(LZUの,给自己的母校打个广告)……
内容:
1、爬取全国的高校的数据;
2、根据天地图的接口,获取高校的经纬度;
3、将数据制作成shp,并转换为geojson;
4、制作全国高校WEBGIS展示图。
效果:
爬取数据,保存为txt
将txt用Excel打开并另存为
在Arcmap中转换为shp
将shp转换为geojson
WEBGIS中展示
实现:
1、获取高校数据
public JSONObject getUrlContent(String url) throws IOException, JSONException{
JSONObject json = null;
InputStream is = null;
try {
is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
String strJson = sb.toString().substring(7,sb.toString().length()-2);
json = new JSONObject(strJson);
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
return json;
}
public String[] getLonLatByName(String name){
String[] lonlat = new String[]{"99","99"};
String url = "http://api.tianditu.com/apiserver/ajaxproxy?proxyReqUrl=http://map.tianditu.com/query.shtml?postStr={'keyWord':'"+name+"','level':'3','mapBound':'29.88281,-23.56399,170.50781,53.54031','queryType':'7','start':'0','count':'1'}&type=query";
InputStream is = null;
try {
is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
String strJson = sb.toString().substring(19,sb.toString().length()-1);
JSONObject json = new JSONObject(strJson);
JSONArray arr = new JSONArray();
if(!json.isNull("pois")){
arr = json.getJSONArray("pois");
JSONObject poiinfo = (JSONObject) arr.get(0);
lonlat = poiinfo.get("lonlat").toString().split(" ");
is.close();
}
}
catch (IOException | JSONException e) {
e.printStackTrace();
}
return lonlat;
}
2、用GDAL实现shp转geojson
# -*- coding: utf-8 -*-
import sys
from osgeo import gdal
from osgeo import ogr
#读取shap文件
def shp2json():
#为了支持中文路径,请添加下面这句代码
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","NO")
#为了使属性表字段支持中文,请添加下面这句
gdal.SetConfigOption("SHAPE_ENCODING","")
#注册所有的驱动
ogr.RegisterAll()
#数据格式的驱动
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.Open(r'D:\data\gdal\university.shp');
if ds is None:
print "打开文件失败!"
sys.exit(1)
dv = ogr.GetDriverByName("GeoJSON")
if dv is None:
print "打开驱动失败!"
sys.exit(1)
dv.CopyDataSource(ds, r"D:\data\gdal\university.geojson")
ds.Destroy()
print "转换成功!"
def main():
shp2json();
if __name__ == "__main__":
main();
3、展示数据
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>百度样式地图</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.1.1/css/ol.css" type="text/css">
<style type="text/css">
body, #map {
border: 0px;
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
font-size: 13px;
overflow: hidden;
}
#map{
background: url("../../images/bgImg.gif");
background-repeat: inherit;
}
</style>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
<script type="text/javascript" src="../../../../plugin/jquery/jquery-1.8.3.js"></script>
<script type="text/javascript">
function init(){
var projection = ol.proj.get("EPSG:3857");
var resolutions = [];
for(var i=0; i<19; i++){
resolutions[i] = Math.pow(2, 18-i);
}
var tilegrid = new ol.tilegrid.TileGrid({
origin: [0,0],
resolutions: resolutions
});
function getBaiduCustomimage(customid){
var baidu_source = new ol.source.TileImage({
projection: projection,
tileGrid: tilegrid,
tileUrlFunction: function(tileCoord, pixelRatio, proj){
if(!tileCoord){
return "";
}
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
if(x<0){
x = "M"+(-x);
}
if(y<0){
y = "M"+(-y);
}
return "http://api2.map.bdimg.com/customimage/tile?&x="+x+
"&y="+y+"&z="+z+
"&udt=20170428&scale=1&ak=E4805d16520de693a3fe707cdc962045&customid="+customid;
}
});
return new ol.layer.Tile({
source: baidu_source
});
}
/**
* 默认地图样式(normal)
* 清新蓝风格(light)
* 黑夜风格(dark)
* 红色警戒风格(redalert)
* 精简风格(googlelite)
* 自然绿风格(grassgreen)
* 午夜蓝风格(midnight)
* 浪漫粉风格(pink)
* 青春绿风格(darkgreen)
* 清新蓝绿风格(bluish)
* 高端灰风格(grayscale)
* 强边界风格(hardedge)
*/
var baidu_layer = getBaiduCustomimage("midnight");
var province = new ol.layer.Image({
source: new ol.source.ImageWMS({
ratio: 1,
url: 'http://10.16.48.185:8086/geoserver/bj_grid/wms',
params: {
'FORMAT': 'image/png',
'VERSION': '1.1.1',
STYLES: '',
LAYERS: 'bj_grid:province_line',
}
})
});
$.get("../data/university.geojson",null,function(result){
result = eval("("+result+")");
var features = (new ol.format.GeoJSON()).readFeatures(result);
for(var i=0;i<features.length;i++){
var _feature = features[i];
var _geom = features[i].getGeometry();
_geom.transform('EPSG:4326', 'EPSG:3857');
_feature.setGeometry(_geom);
}
var vectorSource = new ol.source.Vector({
features: features
});
var vector = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 3,
fill: new ol.style.Fill({
color: "rgba(0,255,255,0.5)"
})
})
})
});
var map = new ol.Map({
target: 'map',
layers: [baidu_layer, province, vector],
view: new ol.View({
center: ol.proj.transform([104.214, 35.847], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
});
}
</script>
</head>
<body onLoad="init()">
<div id="map">
</div>
</body>
</html>