Skip to content

Commit

Permalink
added support for uppercase table names, replaced markerwithlabel wit…
Browse files Browse the repository at this point in the history
…h anycluster_marker.js
  • Loading branch information
biodiv committed Jul 1, 2014
1 parent 898c0f0 commit 3144326
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 107 deletions.
10 changes: 5 additions & 5 deletions anycluster/MapClusterer.py
Expand Up @@ -137,7 +137,7 @@ def __init__(self, zoom=1, gridSize=256, input_srid=4326, mapTileSize=256):
# read the srid of the database.
def getDatabaseSRID(self):

srid_qry = "SELECT id, ST_SRID(%s) FROM %s LIMIT 1;" % (
srid_qry = 'SELECT id, ST_SRID(%s) FROM "%s" LIMIT 1;' % (
geo_column_str, geo_table)
srid_db_objs = Gis.objects.raw(srid_qry)

Expand Down Expand Up @@ -576,7 +576,7 @@ def kmeansCluster(self, clustercells, filters):
'''SELECT kmeans AS id, count(*), ST_Centroid(ST_Collect(%s)) AS %s %s
FROM (
SELECT %s kmeans(ARRAY[ST_X(%s), ST_Y(%s)], 6) OVER (), %s
FROM %s
FROM "%s"
WHERE ST_Within(%s, ST_GeomFromText('%s',%s) ) %s
) AS ksub
GROUP BY kmeans
Expand Down Expand Up @@ -672,14 +672,14 @@ def gridCluster(self, clustercells, filters):

filterstring = self.constructFilterstring(filters)

pin_count_pre = Gis.objects.raw(''' SELECT COUNT(*) AS id FROM %s WHERE ST_Within(%s, ST_GeomFromText('%s',%s) )
pin_count_pre = Gis.objects.raw(''' SELECT COUNT(*) AS id FROM "%s" WHERE ST_Within(%s, ST_GeomFromText('%s',%s) )
%s
''' % (geo_table, geo_column_str, poly, self.srid_db, filterstring))

pin_count = int(pin_count_pre[0].id)

if PINCOLUMN is not None and pin_count == 1:
pinimg_pre = Gis.objects.raw(''' SELECT %s AS id, %s AS %s FROM %s WHERE ST_Within(%s, ST_GeomFromText('%s',%s) )
pinimg_pre = Gis.objects.raw(''' SELECT %s AS id, %s AS %s FROM "%s" WHERE ST_Within(%s, ST_GeomFromText('%s',%s) )
%s
''' % (PINCOLUMN, geo_column_str, geo_column_str, geo_table, geo_column_str, poly, self.srid_db, filterstring))

Expand Down Expand Up @@ -781,7 +781,7 @@ def getKmeansClusterContent(self, x, y, kmeansList, filters):
entries = Gis.objects.raw('''SELECT *
FROM (
SELECT kmeans(ARRAY[ST_X(%s), ST_Y(%s)], 6) OVER (), %s.*
FROM %s
FROM "%s"
WHERE ST_Within(%s, ST_GeomFromText('%s',%s) ) %s
) AS ksub
WHERE kmeans IN (%s);
Expand Down
23 changes: 23 additions & 0 deletions anycluster/static/anycluster/anycluster.css
@@ -0,0 +1,23 @@
.clusterMarkerText {
font-size: 11px;
color: #fff;
font-weight: bold;
}

.cluster-5 {
}

.cluster-10 {
}

.cluster-50 {
}

.cluster-100 {
}

.cluster-1000 {
}

.cluster-10000 {
}
108 changes: 6 additions & 102 deletions anycluster/static/anycluster/anycluster.js
Expand Up @@ -13,6 +13,8 @@ var markerImageSizes = {
var Anycluster = function(mapdiv_id, mapOptions){

var clusterer = this;

this.settings = mapOptions;

this.onFinalClick = mapOptions.onFinalClick;

Expand Down Expand Up @@ -82,107 +84,9 @@ var Anycluster = function(mapdiv_id, mapOptions){
var count = cluster['count'];
var pinimg = cluster['pinimg'];
var ids = cluster["ids"];

var anchor,
icon,
piniconObj = clusterer.selectPinIcon(count,pinimg),
label_content;

var pinicon = piniconObj.url;
var pinsize = piniconObj.size;

if (count == 1){

icon = new google.maps.MarkerImage(pinicon,
// second line defines the dimensions of the image
new google.maps.Size(pinsize[0], pinsize[2]),
// third line defines the origin of the custom icon
new google.maps.Point(0,0),
// and the last line defines the offset for the image
new google.maps.Point(pinsize[0]/2,pinsize[1])
);
anchor = new google.maps.Point(4,9);
label_content = '';

}
else if (count > 10000){
icon = new google.maps.MarkerImage(pinicon,
new google.maps.Size(pinsize[0], pinsize[1]),
new google.maps.Point(0,0),
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(16,9);
label_content = count;
}
var marker = new clusterMarker(center, count, clusterer.gmap, ids);

else if (count > 1000) {
icon = new google.maps.MarkerImage(pinicon,
new google.maps.Size(pinsize[0], pinsize[1]),
new google.maps.Point(0,0),
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(12,9);
label_content = count;
}

else if (count > 100) {
icon = new google.maps.MarkerImage(pinicon,
new google.maps.Size(pinsize[0], pinsize[1]),
new google.maps.Point(0,0),
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(9,9);
label_content = count;
}

else if (count > 50) {
icon = new google.maps.MarkerImage(pinicon,
new google.maps.Size(pinsize[0], pinsize[1]),
new google.maps.Point(0,0),
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(6,9);
label_content = count;
}

else if (count > 10) {
icon = new google.maps.MarkerImage(pinicon,
new google.maps.Size(pinsize[0], pinsize[1]),
new google.maps.Point(0,0),
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(6,9);
label_content = count;
}

else {
icon = new google.maps.MarkerImage(pinicon,
// second line defines the dimensions of the image
new google.maps.Size(pinsize[0], pinsize[1]),
// third line defines the origin of the custom icon
new google.maps.Point(0,0),
// and the last line defines the offset for the image
new google.maps.Point(pinsize[0]/2, pinsize[1]/2)
);
anchor = new google.maps.Point(4,9);
label_content = count;
}

var marker = new MarkerWithLabel({
position: center,
latitude: center.lat(),
longitude: center.lng(),
map: clusterer.gmap,
draggable: false,
labelContent: label_content,
icon: icon,
labelAnchor: anchor,
labelClass: "clusterlabels", // the CSS class for the label
labelInBackground: false,
count: count,
ids: ids
});

clusterer.gridCells.push(marker);

if (clusterer.zoom >= 13 || count <= 3) {
Expand Down Expand Up @@ -498,7 +402,7 @@ Anycluster.prototype = {
markerClickFunction : function(marker) {

this.removeMarkerCells();
this.setMap(marker.longitude, marker.latitude);
this.setMap(marker.center.lng(), marker.center.lat());

},

Expand Down Expand Up @@ -706,8 +610,8 @@ Anycluster.prototype = {

var singlePinURL = "/static/anycluster/images/pin_unknown.png";

if (anyclusterSettings.singlePinImages.hasOwnProperty(pinimg)){
singlePinURL = anyclusterSettings.singlePinImages[pinimg];
if (this.settings.singlePinImages.hasOwnProperty(pinimg)){
singlePinURL = this.settings.singlePinImages[pinimg];
}

}
Expand Down
141 changes: 141 additions & 0 deletions anycluster/static/anycluster/anycluster_marker.js
@@ -0,0 +1,141 @@
// set cluster images depending on count here
var anyclusterImages = {
5 : '/static/anycluster/images/5_empty.png',
10: '/static/anycluster/images/10_empty.png',
50: '/static/anycluster/images/50_empty.png',
100: '/static/anycluster/images/100_empty.png',
1000: '/static/anycluster/images/1000_empty.png',
10000: '/static/anycluster/images/10000_empty.png'
}


/** @constructor */
function clusterMarker(latlng, count, map, ids) {

// Initialize all properties.
this.center = latlng;
this.ids = ids;
this.count_ = count;
this.rounded_count_ = this.roundMarkerCount(count);
this.map_ = map;

// Define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null;

// Explicitly call setMap on this overlay.
this.setMap(map);

};

clusterMarker.prototype = new google.maps.OverlayView();

clusterMarker.prototype.roundMarkerCount = function(){
var count;

if (this.count_ == 1){
count = 1;
}
else if (this.count_ <= 5) {
count = 5;
}
else if (this.count_ <= 10) {
count = 10;
}
else if (this.count_ <= 50) {
count = 50;
}
else if (this.count_ <= 100) {
count = 100;
}
else if (this.count_ <= 1000) {
count = 1000;
}
else {
count = 10000
}

return count;
}


/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
clusterMarker.prototype.onAdd = function() {

var div = document.createElement('div');
div.style.position = 'absolute';

// Create the img element and attach it to the div.
var img = document.createElement('img');
img.src = anyclusterImages[this.rounded_count_];
div.appendChild(img);

var labelDiv = document.createElement('div');
labelDiv.style.textAlign = 'center';
labelDiv.style.position = 'absolute';
labelDiv.style.top = "0px";
labelDiv.style.left = "0px";
labelDiv.className = "clusterMarkerText cluster-" + this.rounded_count_;
labelDiv.textContent = this.count_;
div.appendChild(labelDiv)

this.div_ = div;
this.labelDiv_ = labelDiv;

// Add the element to the "overlayMouseTarget" pane for making clicks possible.
var panes = this.getPanes();
panes.overlayMouseTarget.appendChild(div);

// set this as locally scoped var so event does not get confused
var me = this;

// Add a listener - we'll accept clicks anywhere on this div, but you may want
// to validate the click i.e. verify it occurred in some portion of your overlay.
google.maps.event.addDomListener(div, 'click', function() {
google.maps.event.trigger(me, 'click');
});


};


clusterMarker.prototype.draw = function() {

// We use the south-west and north-east
// coordinates of the overlay to peg it to the correct position and size.
// To do this, we need to retrieve the projection from the overlay.
var overlayProjection = this.getProjection();

// Transform the center into pixel position on map
var pos = overlayProjection.fromLatLngToDivPixel(this.center);

// Resize the image's div to fit the indicated dimensions.
var div = this.div_;

var xOffset = this.div_.clientWidth / 2;
var yOffset = this.div_.clientHeight / 2;

var x = pos.x - xOffset;
var y = pos.y - yOffset

div.style.left = x + 'px';
div.style.top = y + 'px';

var labelDiv = this.labelDiv_;
labelDiv.style.width = div.clientWidth + "px";
labelDiv.style.lineHeight = div.clientHeight + "px";

};

// The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.

clusterMarker.prototype.onRemove = function() {
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};

0 comments on commit 3144326

Please sign in to comment.