flask将{"y":123.2885100,"x":42.7373900,"label":8}格式的json数据传给leaflet,在js文件中写名为dbscan的函数绘制聚类图
时间: 2023-09-11 14:03:48 浏览: 158
flask框架json数据的拿取和返回操作示例
在Flask中,可以使用以下代码将json数据传递给前端:
```python
from flask import Flask, render_template, jsonify
app = Flask(__name__)
@app.route('/')
def index():
data = [{"y":123.2885100,"x":42.7373900,"label":8}, {"y":120.9568800,"x":31.6543800,"label":3}, {"y":116.4071700,"x":39.9046900,"label":5}]
return render_template('index.html', data=data)
if __name__ == '__main__':
app.run(debug=True)
```
然后,在前端HTML文件中引入leaflet和自定义的js文件,并使用以下代码将数据传递给js文件:
```html
<!DOCTYPE html>
<html>
<head>
<title>Flask Leaflet Example</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" integrity="sha512-9gE+Rj0Zp4Jl4O1b4n4vLGqz2IJh1qL+VgX9PdZTLVQ3uq88LM8K7J5m6C5G5UkR6jK1rX9DdJNlY0fNvwN8w==" crossorigin=""/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js" integrity="sha512-0/kcGvRs22HfiztIQP8c2W9Xv1L+2Zlsq3L8C7V5oYL0Rl5y5pOyOj7r9X6DjNgYRZGKJ4c4f4k4aZzQjD3q3A==" crossorigin=""></script>
<script src="{{ url_for('static', filename='dbscan.js') }}"></script>
</head>
<body>
<div id="mapid" style="height: 500px;"></div>
<script>
var data = {{ data | tojson }};
dbscan(data);
</script>
</body>
</html>
```
在自定义的js文件中,可以使用以下代码实现dbscan聚类图的绘制:
```javascript
function dbscan(data) {
// 定义聚类的参数
var epsilon = 0.1;
var minPts = 3;
// 定义一个颜色数组,用于不同聚类之间的区分
var colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF'];
// 定义一个空的聚类结果数组
var clusters = [];
// 遍历所有数据点,进行聚类
for (var i = 0; i < data.length; i++) {
// 如果该点已经被聚类过了,则跳过
if (data[i].cluster !== undefined) {
continue;
}
// 找到该点周围所有距离小于epsilon的点
var neighbors = [];
for (var j = 0; j < data.length; j++) {
if (getDistance(data[i], data[j]) < epsilon) {
neighbors.push(j);
}
}
// 如果该点周围的点数量小于minPts,则将该点标记为噪声点
if (neighbors.length < minPts) {
data[i].cluster = -1;
} else {
// 否则,将该点加入一个新的聚类中,并将该聚类中所有点的cluster属性设置为该聚类的编号
var clusterId = clusters.length;
clusters.push([i]);
data[i].cluster = clusterId;
// 遍历该点周围的所有点,将它们加入同一个聚类中
for (var k = 0; k < neighbors.length; k++) {
var neighborIndex = neighbors[k];
if (data[neighborIndex].cluster === undefined) {
clusters[clusterId].push(neighborIndex);
data[neighborIndex].cluster = clusterId;
}
}
}
}
// 绘制聚类图
var map = L.map('mapid').setView([39.90469, 116.40717], 5);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'your.mapbox.access.token'
}).addTo(map);
for (var i = 0; i < clusters.length; i++) {
// 绘制一个聚类的圆形区域
var latlngs = [];
for (var j = 0; j < clusters[i].length; j++) {
var point = data[clusters[i][j]];
latlngs.push([point.x, point.y]);
}
var circle = L.circle(latlngs[0], {
radius: epsilon * 100000,
color: colors[i % colors.length],
fillOpacity: 0.2
}).addTo(map);
// 绘制该聚类中所有点的标记
for (var j = 0; j < clusters[i].length; j++) {
var point = data[clusters[i][j]];
L.marker([point.x, point.y]).bindPopup('Cluster ' + i).addTo(map);
}
}
}
// 计算两个点之间的距离
function getDistance(p1, p2) {
var dx = p1.x - p2.x;
var dy = p1.y - p2.y;
return Math.sqrt(dx * dx + dy * dy);
}
```
阅读全文