聚类统计图

概述:

前天有网友提到了这样的需求:1、地图的统计图展示;2、统计图的聚类。统计图的展示非常好理解,但是什么是统计图的聚类的?所谓统计图的聚类是按照地图等级与数据等级,实现统计图的分级展示。鉴于此,趁着这个霾天,早起来给这位网友解下惑,并在此marker一下,有相同需求的筒子可以看过来^_^


实现思路:

1、数据组织

因为是分级,所以,经过一番思考,觉得数据通过树形的方式来组织是比较方便使用的。

2、地图展示

地图展示其实是很简单的,例如在0-1级,展示第一级数据;在2-4级展示第二级数据;5级以上展示第三级数据,等等。这个分级规则可以按照地图的世纪情况来确定。

备注:参照代码理解。


实现效果:




实现代码:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>openlayers map</title>
    <link rel="stylesheet" href="../../../plugin/OpenLayers-2.13.1/theme/default/style.css" type="text/css">
    <style>
        html, body, #map{
            padding:0;
            margin:0;
            height:100%;
            width:100%;
            overflow: hidden;
        }
        .tool{
            position: absolute;
            top:10pt;
            right: 10pt;
            padding: 5px;
            background: #fff;
            border: 1px solid #ff5500;
            z-index: 1000;
        }
    </style>
    <script src="../../../plugin/OpenLayers-2.13.1/OpenLayers.js"></script>
    <script src="../../../plugin/jquery/jquery-1.8.3.js"></script>
    <script src="../../../plugin/highcharts/highcharts.js"></script>
    <script>
        var chartData = [{name:"中国",x:103.584297498,y:36.1190864503,data:[
                {
                    name: '男',
                    y: 164.0,
                    color:"#5ab1ef"
                },{
                    name: '女',
                    y: 236.0,
                    color:"#d87a80"
                }
            ],
            children:[
	            {name:"乌鲁木齐",x:87.5758285931,y:43.7822116460,data:[
		            {
		                name: '男',
		                y: 40.0,
		                color:"#5ab1ef"
		            },{
		                name: '女',
		                y: 60.0,
		                color:"#d87a80"
		            }
	        	]},
	            {name:"拉萨",x:91.1629975040,y:29.7104204643,data:[
	                {
	                    name: '男',
	                    y: 45.0,
	                    color:"#5ab1ef"
	                },{
	                    name: '女',
	                    y: 55.0,
	                    color:"#d87a80"
	                }
	            ]},
	            {name:"北京",x:116.4575803581078,y:40.04054437977018,data:[
	                {
	                    name: '男',
	                    y: 35.0,
	                    color:"#5ab1ef"
	                },{
	                    name: '女',
	                    y: 65.0,
	                    color:"#d87a80"
	                }
	            ]},
	            {name:"兰州",x:103.584297498,y:36.1190864503,data:[
	                {
	                    name: '男',
	                    y: 44.0,
	                    color:"#5ab1ef"
	                },{
	                    name: '女',
	                    y: 56.0,
	                    color:"#d87a80"
	                }
	            ]}
            ]
        }];
        var map;
        OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
        OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
        $(window).load(function() {
            var format = 'image/png';
            var bounds = new OpenLayers.Bounds(
                    73.45100463562233, 18.16324718764174,
                    134.97679764650596, 53.531943152223576
            );
            var options = {
                controls: [],
                maxExtent: bounds,
                maxResolution: 0.2403351289487642,
                projection: "EPSG:4326",
                units: 'degrees'
            };
            map = new OpenLayers.Map('map', options);
            var time = new Date();
            time = time.getTime();
            var tiled = new OpenLayers.Layer.WMS(
                    "province", "http://localhost:8088/geoserver/lzugis/wms",
                    {
                        "LAYERS": 'province',
                        "STYLES": '',
                        format: 'image/png'
                    },
                    {
                        buffer: 0,
                        displayOutsideMaxExtent: true,
                        isBaseLayer: true,
                        yx : {'EPSG:4326' : true}
                    }
            );
            map.addLayers([tiled]);
            map.addControl(new OpenLayers.Control.Navigation());
            map.zoomToExtent(bounds);

            $("#addchart").on("click",function(){
                addMapChart();
                map.events.register("zoomend", map, function(){
                    addMapChart();
                });
            });
        });
        function addMapChart(){
            $(".olPopup").remove();
            var zoom = map.getZoom();
            var data = zoom>2?chartData[0].children:chartData;
            for(var i=0;i<data.length;i++){
                var d = data[i];
                var size=120+zoom*8;                
                var domid = "chart"+i;
                var content = "<div class='mapChart' id='"+domid+"' ></div>";
                var popup = new OpenLayers.Popup(domid,
                        new OpenLayers.LonLat(d.x,d.y),
                        new OpenLayers.Size(size,size),
                        content,
                        false);
                popup.setBackgroundColor("transparent");
                popup.setBorder("0px #0066ff solid");
                popup.keepInMap = false;
                map.addPopup(popup,false);
                addChart(domid,d,size);
            }
            var pops = $(map.div).find(".olPopup");
            for(var i=0;i<pops.length;i++){
                var pop = $(pops[i]);
                var top = pop.position().top,
                        left = pop.position().left;
                pop.css("top",(top-size/2)+"px").css("left",(left-size/2)+"px");
            }
        }
        function addChart(domid,data,size){
            $('#'+domid).highcharts({
                chart: {
                    backgroundColor: 'rgba(255, 255, 255, 0)',
                    plotBorderColor: null,
                    plotBackgroundColor: null,
                    plotBackgroundImage: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    width: size,
                    height: size
                },
                tooltip: {
                    pointFormat: '<b>{point.percentage:.1f}%</b>'
                },
                credits:{
                    enabled:false
                },
                title: {
                    text: ''
                },
                plotOptions:{
                    pie: {
                        dataLabels: {
                            enabled: false
                        }
                    }
                },
                series: [{
                    type: 'pie',
                    name: data.name,
                    data: data.data
                }]
            });
        }
    </script>
</head>
<body>
<div id="map">
    <div class="tool">
        <button id="addchart">添加统计图</button>
    </div>
    <div style="display: none;" id="chart">
    </div>
</div>
</body>
</html>

---------------------------------------------------------------------------------------------------------------

技术博客

http://blog.csdn.NET/gisshixisheng

在线教程

http://edu.csdn.Net/course/detail/799
Github

https://github.com/lzugis/

联系方式

q       q:1004740957

e-mail:niujp08@qq.com

公众号:lzugis15

Q Q 群:452117357(webgis)
             337469080(Android)


### 使用像处理技术进行像素值的聚类分析 对于像处理中的像素值聚类分析,常用的方法之一是利用 k 均值 (k-means) 聚类算法。该算法能够有效地将像按照颜色或其他特征分为多个簇群,使得同一簇内的像素尽可能相似。 #### 数据准备与预处理 为了执行有效的聚类操作,首先需要加载并转换数据至适合计算的形式: ```python import numpy as np from PIL import Image import matplotlib.pyplot as plt def load_image(image_path): img = Image.open(image_path) data = np.array(img, dtype=np.float64) / 255 # 归一化到0-1区间 w, h, d = tuple(data.shape) assert d == 3, "仅支持彩色像" image_array = np.reshape(data, (w * h, d)) return image_array, w, h ``` 此函数读取指定路径下的片文件,并将其转化为浮点数数组形式以便后续处理[^2]。 #### 执行 K-Means 聚类 接下来,使用 `sklearn` 库提供的 KMeans 类来进行实际的聚类工作: ```python from sklearn.cluster import KMeans n_clusters = 8 # 设定期望得到的颜色数量 image_array, width, height = load_image('path_to_your_image.jpg') model = KMeans(n_clusters=n_clusters, random_state=42).fit(image_array) labels = model.predict(image_array) cluster_centers = model.cluster_centers_ ``` 上述代码片段展示了如何设置模型参数以及训练过程[^1]。 #### 结果可视化 最后一步是对结果进行展示,这有助于直观理解聚类效果: ```python def recreate_image(cluster_centers, labels, w, h): """重建原始尺寸的像""" d = cluster_centers.shape[1] image_clustered = np.zeros((w, h, d), dtype=float) label_idx = 0 for i in range(w): for j in range(h): image_clustered[i][j] = cluster_centers[labels[label_idx]] label_idx += 1 return image_clustered plt.figure(figsize=(12, 6)) # 显示原 plt.subplot(1, 2, 1) original_img = Image.open('path_to_your_image.jpg') plt.imshow(original_img) plt.title('Original Image', fontsize=20) # 显示聚类后的像 plt.subplot(1, 2, 2) recreated_img = recreate_image(cluster_centers, labels, width, height) plt.imshow(recreated_img) plt.title(f'Clustered Image ({n_clusters} colors)', fontsize=20) plt.show() ``` 这段脚本会创建两个子表分别显示原始像及其经过简化色彩后的版本。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛老师讲GIS

感谢老板支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值