Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking up the code #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def getData():
print "received coordinates: [" + lat1 + ", " + lat2 + "], [" + lng1 + ", " + lng2 + "]"

client = pyorient.OrientDB("localhost", 2424)
session_id = client.connect("root", "password")
db_name = "property_test"
session_id = client.connect("root", "474F1CBE549F9E33FC8A0793C7819485072DAE07A4B704138010F28A28B69DF9")
db_name = "soufun"
db_username = "admin"
db_password = "admin"

Expand Down Expand Up @@ -84,4 +84,4 @@ def getData():
return json.dumps(output)

if __name__ == "__main__":
app.run(host='0.0.0.0',port=5000,debug=True,threaded=True)
app.run(host='0.0.0.0',port=4000,debug=True,threaded=True)
112 changes: 112 additions & 0 deletions static/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var eventOutputContainer = document.getElementById("message");
var eventSrc = new EventSource("/eventSource");

eventSrc.onmessage = function(e) {
console.log(e);
eventOutputContainer.innerHTML = e.data;
};

var tooltip = d3.select("div.tooltip");
var tooltip_title = d3.select("#title");
var tooltip_price = d3.select("#price");


var map = L.map('map').setView([22.539029, 114.062076], 16);

//this is the OpenStreetMap tile implementation

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

//uncomment for Mapbox implementation, and supply your own access token

// L.tileLayer('https://api.tiles.mapbox.com/v4/{mapid}/{z}/{x}/{y}.png?access_token={accessToken}', {
// attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
// mapid: 'mapbox.light',
// accessToken: [INSERT YOUR TOKEN HERE!]
// }).addTo(map);

//create variables to store a reference to svg and g elements

var svg = d3.select(map.getPanes().overlayPane).append("svg");
var g = svg.append("g").attr("class", "leaflet-zoom-hide");

function projectPoint(lat, lng) {
return map.latLngToLayerPoint(new L.LatLng(lat, lng));
}

function projectStream(lat, lng) {
var point = projectPoint(lat,lng);
this.stream.point(point.x, point.y);
}

var transform = d3.geo.transform({point: projectStream});
var path = d3.geo.path().projection(transform);

function updateData(){

var mapBounds = map.getBounds();
var lat1 = mapBounds["_southWest"]["lat"];
var lat2 = mapBounds["_northEast"]["lat"];
var lng1 = mapBounds["_southWest"]["lng"];
var lng2 = mapBounds["_northEast"]["lng"];

request = "/getData?lat1=" + lat1 + "&lat2=" + lat2 + "&lng1=" + lng1 + "&lng2=" + lng2

console.log(request);

d3.json(request, function(data) {

//create placeholder circle geometry and bind it to data
var circles = g.selectAll("circle").data(data.features);

circles.enter()
.append("circle")
.on("mouseover", function(d){
tooltip.style("visibility", "visible");
tooltip_title.text(d.properties.name);
tooltip_price.text("Price: " + d.properties.price);
})
.on("mousemove", function(){
tooltip.style("top", (d3.event.pageY-10)+"px")
tooltip.style("left",(d3.event.pageX+10)+"px");
})
.on("mouseout", function(){
tooltip.style("visibility", "hidden");
})
;

// function to update the data
function update() {

// get bounding box of data
var bounds = path.bounds(data),
topLeft = bounds[0],
bottomRight = bounds[1];

var buffer = 50;

// reposition the SVG to cover the features.
svg .attr("width", bottomRight[0] - topLeft[0] + (buffer * 2))
.attr("height", bottomRight[1] - topLeft[1] + (buffer * 2))
.style("left", (topLeft[0] - buffer) + "px")
.style("top", (topLeft[1] - buffer) + "px");

g .attr("transform", "translate(" + (-topLeft[0] + buffer) + "," + (-topLeft[1] + buffer) + ")");

// update circle position and size
circles
.attr("cx", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).x; })
.attr("cy", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).y; })
.attr("r", function(d) { return Math.pow(d.properties.price,.3); });
};

// call function to
update();
map.on("viewreset", update);
});

};

updateData();
49 changes: 49 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
body {
padding: 0;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
line-height: 18px;
}
html, body, #map {
height: 100%;
}

circle {
fill-opacity: .3;
fill: red;
stroke-width: 0px;
}
circle:hover {
stroke: black;
stroke-width: 2px;
cursor: crosshair;
}

div.tooltip{
padding: 6px;
background: white;
visibility: hidden;
position: absolute;
z-index: 10;
}
p.tooltip-text{
margin: 0px;
padding: 0px;
}

div.menu{
position: fixed;
margin: 0px;
padding: 10px;
top: 0px;
right: 0px;
width: 250px;
height: 500px;
background: rgba(255,255,255,.8);
}

em{
color: red;
font-weight: bold;
}
173 changes: 3 additions & 170 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,61 +1,12 @@
<html>

<head>
<title>DMC Web Stack</title>
<script type="text/javascript" src="./static/lib/d3.js"></script>
<script type="text/javascript" src="./static/lib/leaflet.js"></script>
<link rel="stylesheet" href="./static/lib/leaflet.css" />

<style>
body {
padding: 0;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
line-height: 18px;
}
html, body, #map {
height: 100%;
}

circle {
fill-opacity: .3;
fill: red;
stroke-width: 0px;
}
circle:hover {
stroke: black;
stroke-width: 2px;
cursor: crosshair;
}

div.tooltip{
padding: 6px;
background: white;
visibility: hidden;
position: absolute;
z-index: 10;
}
p.tooltip-text{
margin: 0px;
padding: 0px;
}

div.menu{
position: fixed;
margin: 0px;
padding: 10px;
top: 0px;
right: 0px;
width: 250px;
height: 500px;
background: rgba(255,255,255,.8);
}

em{
color: red;
font-weight: bold;
}
</style>
<link rel="stylesheet" href="./static/style.css" />

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

Expand All @@ -77,124 +28,6 @@
<p>Status: <em id="message"></em></p>
</form>
</div>


<script type="text/javascript">

var eventOutputContainer = document.getElementById("message");
var eventSrc = new EventSource("/eventSource");

eventSrc.onmessage = function(e) {
console.log(e);
eventOutputContainer.innerHTML = e.data;
};

var tooltip = d3.select("div.tooltip");
var tooltip_title = d3.select("#title");
var tooltip_price = d3.select("#price");


var map = L.map('map').setView([22.539029, 114.062076], 16);

//this is the OpenStreetMap tile implementation

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

//uncomment for Mapbox implementation, and supply your own access token

// L.tileLayer('https://api.tiles.mapbox.com/v4/{mapid}/{z}/{x}/{y}.png?access_token={accessToken}', {
// attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
// mapid: 'mapbox.light',
// accessToken: [INSERT YOUR TOKEN HERE!]
// }).addTo(map);

//create variables to store a reference to svg and g elements

var svg = d3.select(map.getPanes().overlayPane).append("svg");
var g = svg.append("g").attr("class", "leaflet-zoom-hide");

function projectPoint(lat, lng) {
return map.latLngToLayerPoint(new L.LatLng(lat, lng));
}

function projectStream(lat, lng) {
var point = projectPoint(lat,lng);
this.stream.point(point.x, point.y);
}

var transform = d3.geo.transform({point: projectStream});
var path = d3.geo.path().projection(transform);

function updateData(){

var mapBounds = map.getBounds();
var lat1 = mapBounds["_southWest"]["lat"];
var lat2 = mapBounds["_northEast"]["lat"];
var lng1 = mapBounds["_southWest"]["lng"];
var lng2 = mapBounds["_northEast"]["lng"];

request = "/getData?lat1=" + lat1 + "&lat2=" + lat2 + "&lng1=" + lng1 + "&lng2=" + lng2

console.log(request);

d3.json(request, function(data) {

//create placeholder circle geometry and bind it to data
var circles = g.selectAll("circle").data(data.features);

circles.enter()
.append("circle")
.on("mouseover", function(d){
tooltip.style("visibility", "visible");
tooltip_title.text(d.properties.name);
tooltip_price.text("Price: " + d.properties.price);
})
.on("mousemove", function(){
tooltip.style("top", (d3.event.pageY-10)+"px")
tooltip.style("left",(d3.event.pageX+10)+"px");
})
.on("mouseout", function(){
tooltip.style("visibility", "hidden");
})
;

// function to update the data
function update() {

// get bounding box of data
var bounds = path.bounds(data),
topLeft = bounds[0],
bottomRight = bounds[1];

var buffer = 50;

// reposition the SVG to cover the features.
svg .attr("width", bottomRight[0] - topLeft[0] + (buffer * 2))
.attr("height", bottomRight[1] - topLeft[1] + (buffer * 2))
.style("left", (topLeft[0] - buffer) + "px")
.style("top", (topLeft[1] - buffer) + "px");

g .attr("transform", "translate(" + (-topLeft[0] + buffer) + "," + (-topLeft[1] + buffer) + ")");

// update circle position and size
circles
.attr("cx", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).x; })
.attr("cy", function(d) { return projectPoint(d.geometry.coordinates[0], d.geometry.coordinates[1]).y; })
.attr("r", function(d) { return Math.pow(d.properties.price,.3); });
};

// call function to
update();
map.on("viewreset", update);
});

};

updateData();

</script>

<script type="text/javascript" src="./static/script.js"></script>
</body>
</html>