90
Web Information Systems prof. Athena Vakali Informatics Department, AUTh 13 Dec 2011 Geographical Data Management for Web Applications Symeon Papadopoulos

Geographical Data Management for Web Applications

Embed Size (px)

DESCRIPTION

Lecture given at the Informatics Department of the Aristotle University of Thessaloniki on introductory topics of geographical data management for web applications.

Citation preview

Page 1: Geographical Data Management for Web Applications

Web Information Systems

prof. Athena Vakali

Informatics Department, AUTh

13 Dec 2011

Geographical Data Management for Web Applications

Symeon Papadopoulos

Page 2: Geographical Data Management for Web Applications

Web Information Systems

Overview

• Introduction

• Representation & storage of geographical data

• Geographical data management with Java (JDBC)

• Text Indexing for Geographical Data

• Google Maps API

• Use Cases: Geographical Data Clustering + Heat maps

Page 3: Geographical Data Management for Web Applications

The Era of Geo

• 350K websites make use of Google Maps API (2010)

• GPS-enabled mobile phones > 200M (2010)

• Foursquare users: 5M (2010) 15M (2011)

• The HotPotato check-in service was acquired by Facebook for $10Μ (2010)

Web Information Systems

Page 4: Geographical Data Management for Web Applications

Categories of Geo-Services

• Primary services (providers)

– Google Maps, Bing Maps, Yahoo Maps, MapQuest, OpenStreetMap

• Derivative services (mash-ups)

• Location-based social networks

– Foursquare, Gowalla (acquired by Facebook), Brightkite

• Intelligent geo-based services

– Analytics, Route planning, Real estate

Web Information Systems

Page 5: Geographical Data Management for Web Applications

GIS

• GIS offer sophisticated software for geographical data management and processing.

• They offer many means of representation, search and rendering.

• They mostly target geo-centered applications:

– Urban planning, environmental studies, etc.

• Popular tools: ArcGIS, MapInfo, GeoMedia, κλπ.

• GIS will not be discussed in this lecture.

Web Information Systems

Page 6: Geographical Data Management for Web Applications

Representation and Storage of Geographical Data

Page 7: Geographical Data Management for Web Applications

Lat, Lon

• Geo coordinates constitute the basic element of any geo-enabled service.

• A coordinate element is defined by the following:

Latitude Longitude

Web Information Systems

Page 8: Geographical Data Management for Web Applications

Lat, Lon (ΙΙ)

• Traditional writing:

40° 38′ 0″ N, 22° 57′ 0″ E

• Decimal representation: 40.633333, 22.95

• Important note for storage:

Precision of 0.00001 in the decimal representation corresponds to approximately 1m distance (near the equator)

Web Information Systems

Page 9: Geographical Data Management for Web Applications

Altitude, Bearing

• Two additional variables related to geographical points (still not very common):

– Altitude: vertical distance of point from the level of sea

– Bearing: direction to which the user/device is pointed

• The increasing use of GPS-enabled devices is expected to increase the importance of these variables, especially in Augmented Reality (AR) applications.

Web Information Systems

Page 10: Geographical Data Management for Web Applications

Distance calculation

• Haversine formula: – Δlat = lat2 – lat1, Δlon = lon2 – lon1

– a = sin2(Δlat/2) + cos(lat1) cos(lat2) sin2(Δlon/2) – c = 2 atan2(sqrt(a), sqrt(1-a)) – d = R c [R: earth radius (6 371 km)]

• In JavaScript:

– var R = 6371; // km – var dLat = (lat2-lat1).toRad(); – var dLon = (lon2-lon1).toRad(); – var a = Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon/2) * Math.sin(dLon/2);

– var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); – var d = R * c;

(lat1, lon1)

(lat2, lon2)

d

More accurate and computational expensive alternative: Vincenty formula

Web Information Systems

Page 11: Geographical Data Management for Web Applications

Geo in DB

• Representation in table form

• e.g. List of Points of interest Table of Pois

• double slightly faster

• DECIMAL higher accuracy Field type: double ή DECIMAL(13,9)

Web Information Systems

Page 12: Geographical Data Management for Web Applications

Use cases

• Query 1: What points of interest can I see near point of interest X?

– Online city exploration in cities (e.g. recommend new Pois based on proximity)

• Query 2: What points of interest are there near me?

– Valuable within location-based applications (e.g. I need recommendations for sightseeing, eating, etc. based on my current location)

Web Information Systems

Page 13: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (I)

• Implementation 1 – Compute distance by use of SQL and return ordered results:

• set @lat0 = 40.645466; set @lon0 = 22.858342;

set @dist = 10;

• SELECT *, 6371 2 ASIN ( SQRT( POWER(

SIN(@lat0 – abs(dest.lat)) pi()/180 / 2), 2) +

COS (@lat0 pi()/180 ) COS(abs(dest.lat) pi()/180) POWER( SIN(@lon0 – dest.lon) pi()/180 / 2), 2) )) as distance

FROM pois dest

HAVING distance < @dist

ORDER BY distance LIMIT 10;

Haversine formula

Web Information Systems

Page 14: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (II)

• Problems:

– Painfully slow!

For tables of even modest sizes (~1000), response might take as long as 8-10 seconds.

– Distance computations are not reused in successive invocations of the same query, since caching computations is not supported

– Not an option for web applications.

Web Information Systems

Page 15: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (IIΙ)

• Alternative implementation:

– If, instead of nearby Pois, we ask for Pois that lie within a certain radius from a fixed point (e.g. current location)?

300m

300m Aghia Sophia

Web Information Systems

Page 16: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (IV)

• Bounding box query:

– SELECT * FROM pois WHERE

pois.latitude > @min_lat AND pois.latitude < @max_lat AND pois.longitude > @min_lon AND pois.longitude < @max_lon;

– The question now is how

to determine

min_lat, min_lon,

max_lat, max_lon.

(@min_lat, @min_lon)

(@max_lat, @max_lon)

Web Information Systems

Page 17: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (Vα)

• Bounding box computation (top and bottom):

• 1°difference in latitude ~ 111km (110.567 near the equator, 111.699 near the poles) – 300m ~0.0027° latitude (since our fixed point is

located at approximately 45°)

– @min_lat = @lat0 – 0.0027

– @max_lat = @lat0 + 0.0027

e.g. for lat0 = 40.632844 (Aghia Sophia)

@min_lat ~ 40.630144

@max_lat ~ 40.635544

@min_lat

@max_lat

@lat0

Web Information Systems

Page 18: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (Vβ)

• Bounding box computation (left and right): • 1°difference in longitude depends a lot on the latitude of

the fixed point, e.g ~111.320 km near the equator and 0 near the poles. – Table with distance correspondences (the more lines, the better the achieved

approximation – for intermediate values one needs to use interpolation)

– 300m ~0.003805° latitude since the fixed point lies at latitude~45°

– @min_lon = @lon0 – 0.003805 – @max_lon = @lon0 + 0.003805 – e.g. for lon0 = 22.947094 (Aghia Sophia) @min_lon ~ 22.943289 @max_lon ~ 22.950899

@min_lon

@max_lon

@lon0

Web Information Systems

Distance (km) for 1° difference in longitude

Page 19: Geographical Data Management for Web Applications

Geo in DB – Proximity Queries (VI)

• In summary:

– During the table creation, we make sure to index the latitude and longitude columns (conventional B-tree).

– We convert the proximity query to a bounding box query by use of geographical approximations (that can be made sufficiently precise if needed).

– In this form, the SQL query can be executed really fast thanks to the indexes.

Web Information Systems

Page 20: Geographical Data Management for Web Applications

Geo-enabled DBs

• PostGIS (Extension for PostgreSQL) – Support for “geographical objects”:

• POINT (X Y)

• LINESTRING (0 0, 1 1, 1 2)

• POLYGON (0 0, 4 0, 4 4, 0 4, 0 0)

• MULTIPOINT, MULTIPOLYGON, GEOMETRYCOLLECTION

– Support for tables and calculations (e.g. proximity queries) directly through the query engine.

• GeoServer (GIS) – Complete Java framework for search of geographical

objects and map rendering based on a server-client architecture.

Web Information Systems

Page 21: Geographical Data Management for Web Applications

Geographical data management in Java (JDBC)

Page 22: Geographical Data Management for Web Applications

Processing

RDBMS storage

Typical lifecycle of geographical data

Geo data source (e.g. Wikimapia)

Transformation to records

JDBC

Relational model specification

READ UPDATE

JAVA

JDBC

Web Information Systems

Page 23: Geographical Data Management for Web Applications

Data model specification

• Very simple example: table with id + 3 columns – Name: VARCHAR (100) – Latitude: DOUBLE – Longitude: DOUBLE

Pois

Web Information Systems

Page 24: Geographical Data Management for Web Applications

Input data + transformation

<name>Benaki museum</name> “Benaki Museum”

<lon>23.70825</lon> 23.70825 <lat>37.974515</lat> 37.974515

name latitude longitude

Benaki museum 23.70825 37.974515

Record

parsing

Web Information Systems

Page 25: Geographical Data Management for Web Applications

Record insertion with JDBC (1)

• Step 1 Connection Creation (A) Load the appropriate JDBC driver E.g. in the following example: JDBC_DRIVER = “com.mysql.jdbc.Driver” Class.forName(JDBC_DRIVER); (B) Connection initialization Τhe variable dbURL has the form: protocol + vendor + server + port nr + dbName

+ params e.g. jdbc:mysql//myserver.gr:3306/testdb/? useUnicode=true&characterEncoding=UTF-8

Connection con = DriverManager.getConnection(dbURL, username, password); throws exceptions (InstantiationException, IllegalAccessException,

ClassNotFoundException, SQLException)

ATTENTION: Connection objects should be reused!

Web Information Systems

Page 26: Geographical Data Management for Web Applications

Record insertion with JDBC (2)

• Step 2 Creation and execution of statement (A) Statement creation String sqlInsert = “INSERT INTO Pois VALUES (‘Benaki museum’, 23.70825, 37.974515)”; Statement insertStmt = con.createStatement(); (B) Statement execution (throws SQLException) insertStmt.executeUpdate(sqlInsert); Alternatively PreparedStatement prepInsert = con.prepareStatement(sqlInsert,

Statement.RETURN_GENERATED_KEYS); prepInsertStmt.executeUpdate(); ResultSet rs = prepInsertStmt.getGeneratedKeys(); long recordId = -1; if (rs.next()) recordId = rs.getLong(1);

Record database key could be useful later (better keep it than query for it)

Web Information Systems

Page 27: Geographical Data Management for Web Applications

Wrapper method creation

• Encapsulation of process for easy reuse

public long insertPoi(Connection con, String poiName, double lat, double lon) {

String sqlInsert = “INSERT INTO Pois VALUES (‘” + poiName + “’,” String.valueof(lat) + “,” + String.valueof(lon) + “)”; PreparedStatement prepInsert = con.prepareStatement(sqlInsert,

Statement.RETURN_GENERATED_KEYS); prepInsertStmt.executeUpdate(); ResultSet rs = prepInsertStmt.getGeneratedKeys(); long recordId = -1; if (rs.next()) recordId = rs.getLong(1); return recordId; }

Web Information Systems

Page 28: Geographical Data Management for Web Applications

Batch insertion

• In case of massive insertions, it is better to make use of the batch mode of insertion in order to significantly speed up the process.

con.setAutoCommit(false);

Statement batchInsert = con.createStatement();

for (int i = 0; i < 10,000; i++){

batchInsert.addBatch(“INSERT INTO Pois VALUES (‘“ +

names*i+ + “’,” + String.valueof(lats*i+) + “,” + String.valueof(lons*i+) + “)”;

}

int[] updateCount = batchInsert.executeBatch();

con.commit();

con.setAutoCommit(true);

Very important!!

Web Information Systems

Page 29: Geographical Data Management for Web Applications

ResultSet structure

Initial cursor position C1 C2 C3 C4 C5 C6 C7 ….

22 ab 0.1

27 cd 0.5

28 gc 1.2

40 ob 3.5

51 gz 3.2

R1

R2

R3

R4

R5

rs.next()

rs.getInt(1) rs.getInt(“C1”)

rs.getString(2) (faster) rs.getString(“C2”)

rs.getDouble(3) rs.getDouble(“C3”)

rs.absolute(3)

Web Information Systems

Page 30: Geographical Data Management for Web Applications

Record reading

• Use SELECT statement and iterate through ResultSet (A) SELECT statement creation Statement selectStmt = con.createStatement(); (B) Form and execute SELECT statement List<Poi> pois = new ArrayList<Poi>(); String sqlSelect = “SELECT * FROM Pois”; ResultSet rs = stmt.executeQuery(sqlSelect); while ( rs.next() ) { long poiId = rs.getLong(“id”); String poiName = rs.getString(“name”); double lat = rs.getDouble(“latitude”); double lon = rs.getDouble(“longitude”); Poi poi = new Poi(poiId, poiName, lat, lon); pois.add(poi); }

Candidate wrapper method Poi extractPoiFromResultSet(ResultSet rs)

Web Information Systems

Page 31: Geographical Data Management for Web Applications

Record updates

• Suppose I have collected data about the popularity of a Poi (e.g. count the number of results from a search engine) and I have its id at my disposal:

public void updatePoiPopularity(Connection con, long poiId, int popularity) {

PreparedStatement updateStatement = con.prepareStatement(

“UPDATE Pois SET poi_popularity = ? WHERE id = ?”);

updateStatement.setInt(1, popularity);

updateStatement.setLong(2, poiId);

int n = updateStatement.executeUpdate();

}

name latitude longitude poi_popularity

…. … … …

NEW COLUMN

Web Information Systems

Page 32: Geographical Data Management for Web Applications

PoiDbWrapper

Example Design

• Task separation

List<Poi> pois = WikimapiaWrapper.collectPois(“Thessaloniki”);

for (int i = 0; i < pois.size(); i++){

long poiId = PoiDbWrapper.insertPoi(con, pois.get(i).getName(),

pois.get(i).getLatitude(), pois.get(i).getLongitude());

pois.get(i).setId(poiId);

}

….

Map<Poi,Integer> poiPopMap = PopWrapper.collectPoiPopularities(pois);

for (Entry<Poi,Integer> poiPopEntry : poiPopMap.entrySet()){

PoiDbWrapper.updatePoiPopularity(con,

poiPopEntry.getKey().getId(), poiPopEntry.getValue());

}

WikimapiaWrapper PopWrapper

Web Information Systems

Page 33: Geographical Data Management for Web Applications

Text Indexing for Geographical Data

Page 34: Geographical Data Management for Web Applications

Full-text Indexing

• Extremely valuable technology for applications requiring full-text search features.

• Classical DB systems do not offer a standard means of indexing and searching large amounts of text.

• Established technology: Inverted indices

• Popular implementations: – Lucene, Solr (Java) – Sphinx (C++) – OpenFTS (PostgreSQL) – Lemur (C++)

Web Information Systems

Page 35: Geographical Data Management for Web Applications

Text queries in RDBMS

• Operators LIKE and RLIKE – SELECT * FROM pois WHERE name LIKE ‘%museum%’

– SELECT * FROM pois WHERE name RLIKE ‘museum’

• Conventional indexing structures in RDBMS are not particularly helpful in quickly responding to such queries (might require visiting every record too slow for large tables)

• Each vendor offers own solution, e.g. FULLTEXT INDEX από MySQL.

Web Information Systems

Page 36: Geographical Data Management for Web Applications

Inverted index (I)

• Starting point: Document collection (corpus)

• Text processing

Dictionary: set of unique terms appearing in the documents of the collection + additional information (stats, stems, stop words)

• Index: Document retrieval mechanism based on the terms contained in the documents

Web Information Systems

Page 37: Geographical Data Management for Web Applications

Inverted index (II)

• Inverted index: Set of inverted lists.

• Inverted list: A list containing information about the occurrence of a term within the documents of the collection – represented as a list of postings.

• Posting: Occurrence of a term in a certain position of a document. Apart from the document, where the term occurs, the posting also contains its position in the text support for phrase search (increases the size of the index and complexity of the query mechanism).

Web Information Systems

Page 38: Geographical Data Management for Web Applications

Inverted index (IIΙ)

Example

• Document collection: – Document 1: “White Tower”

– Document 2: “Byzantine Museum”

– Document 3: “Archeological Museum”

• Inverted lists of postings: – “Archeological”: {(3,1)}

– “Byzantine”: {(2,1)}

– “Museum”: {(2,2), (3,2)}

– “White”: {(1,1)}

– “Tower”: {(1,2)}

Document id

Term position within the document

Web Information Systems

Page 39: Geographical Data Management for Web Applications

Inverted index (IV)

Query example

• Query: “Museum” documents 2 and 3

• Query: “Byzantine Museum” – Result 1: document 2 (contains both terms)

– Result 2: document 3 (contains only the term “museum”)

• “Archeological”: {(3,1)} • “Byzantine”: {(2,1)} • “Museum”: {(2,2), (3,2)} • “White”: {(1,1)} • “Tower”: {(1,2)}

• Document 1: “White Tower” • Document 2: “Byzantine Museum” • Document 3: “Archeological Museum”

Web Information Systems

Page 40: Geographical Data Management for Web Applications

Solr (text search engine)

• Complete framework for indexing and searching massive document collections.

• Based on the Lucene library.

• Flexible document representation structure.

• Flexible query structure.

• Can be installed as a servlet in an appropriate container (e.g. Tomcat, Jetty) Possibility for use over HTTP

Web Information Systems

Page 41: Geographical Data Management for Web Applications

Solr basics (Ι)

• Every document consists of a set of fields (similar to Lucene)

• Every field can be of different type (e.g. text, number, etc.)

• Every field can processed (e.g. tokenized, normalized) and indexed in a different way.

• Every field or combination of fields can be queried in a different way flexible query system

Web Information Systems

Page 42: Geographical Data Management for Web Applications

Example: – id: 13424 – name: White Tower of Thessaloniki – latitude: 40.626369 – longitude: 22.948428 – history: The tower, which once guarded the eastern end of the city's sea

walls, was for many years attributed to Venice, to which the Byzantines ceded Thessaloniki in 1423.

– wikipedia_link: http://en.wikipedia.org/wiki/White_Tower_of_Thessaloniki

Xml representation suitable fοr use by Solr:

<add><doc> <field name=“id”>13424</field> <field name=“name”>White Tower of Thessaloniki</field> ... </doc></add>

Solr basics (ΙΙ)

Web Information Systems

Page 43: Geographical Data Management for Web Applications

Solr: index management

• Addition of documents: java -jar post.jar lefkos_pirgos.xml – The xml file should follow the structure of the example in the previous

example. – A list of files can be provided as input (whitespace separated). – If a document with the same id (unique key) is provided as input, then

the new document will replace the existing.

• Deletion of documents: java -Ddata=args -Dcommit=no -jar post.jar

"<delete><id>13424</id></delete>"

• Committing changes: java –jar post.jar

• The above operations (and more) are available through a Java (not only) API as well.

Web Information Systems

Page 44: Geographical Data Management for Web Applications

Solr: Queries

HTTP API: Base URL: http://server:8983/solr/select/?

Return fields “name”, “id” of all entries containing ‘museum’.

• q=museum&fl=name,id

As above but also return relevance score for each entry.

• q=museum&fl=name,id,score

As above but return all stored field.

• q=museum&fl=*,score

Return entries in JSON format.

• q=museum&wt=json

Return all entries containing the keyword ‘pirgos’ in their field “name”.

• q=name:pirgos

Web Information Systems

Page 45: Geographical Data Management for Web Applications

Challenges

• Text indexing and search frequently encounters the following two challenges:

– Synonyms, writing variations: users search for something using a form different than the “reference” form.

e.g. White Tower, Λευκός Πύργος, λευκός πύργος, lefkos pyrgos, ... Solution: Attempt to collect and index many different lexical forms of the same term.

– Polysemy/Ambiguity: A single term refers to more than one concepts.

e.g. the term museum may refer to a number of real-world museums

Solution: Appropriate interfaces are necessary to make the user aware of the different senses/contexts of a term and ask for query reformulation/specification (could also come up with suggestions).

Web Information Systems

Page 46: Geographical Data Management for Web Applications

Synonyms

• Strategy 1 (simple but not always available):

– Use dictionary/thesaurus/gazetteer (e.g. Geonames)

Contains alternative names and variations for a large number of Pois and locations.

– Addition of a “synonyms” field in the index

– Include the additional field in the search

…but give a higher weight to the “name” field.

Web Information Systems

Page 47: Geographical Data Management for Web Applications

Related terms

• Strategy 2 (more complex but generally applicable):

– Issue query to search engine or other content application (e.g. Flickr) • Use Poi name as query.

– Process search results in two ways: • Find terms or term sequences that frequently occur in the same context (e.g. web

page, title of photo, etc.).

• Find terms that are lexically similar to the query term (e.g. based on the Levenshtein distance) .

– Addition of a “similars” field in the index.

– Include the additional field in the search • but give higher weight to the “name” field

Web Information Systems

Page 48: Geographical Data Management for Web Applications

Google Maps API

Page 49: Geographical Data Management for Web Applications

Licensing

• The Google Maps service is free for web pages that are freely available. Charges apply for commercial usage of the API.

• Since V3, the API does not require a key.

Web Information Systems

Page 50: Geographical Data Management for Web Applications

Google Maps API (I)

• The map service is based on the notion of map tiles, which, when stitched together, provide an impression of a continuous map.

Web Information Systems

Page 51: Geographical Data Management for Web Applications

Google Maps API (II)

• There are different types of map types, e.g. ROADMAP, SATELLITE, HYBRID, TERRAIN. Those are called base map tiles.

ROADMAP

HYBRID

HYBRID

TERRAIN

Web Information Systems

Page 52: Geographical Data Management for Web Applications

Google Maps API – Coordinates (I)

Coordinates:

• World: Point 0,0 lat ~ 85°

lon 180°

• Pixel: pixelCoord = worldCoord 2zoomLevel

The API first determines the visible area of the map on the client (viewport) based on the zoom level, the map center and the size (in pixels) of the DOM element in the client web page.

Web Information Systems

Page 53: Geographical Data Management for Web Applications

Google Maps API – Coordinates (II)

Coordinates:

• Tile coordinates: – With every scroll or zoom to a new viewport, the API needs to render

a new area on the map. For this to happen, it needs to translate the pixel coordinates into appropriate tile coordinates in order to load the appropriate tiles. At each zoom level the tile coordinates are different.

– e.g. at zoom level 2 (beside) 16 tiles need to be indexed. The tile containing Greece has coordinates (2,1).

At zoom level 3, 16 x 4 = 64 tiles need to to be indexed, and so on.

Web Information Systems

Page 54: Geographical Data Management for Web Applications

The “hello world” of Google Maps

• The map object is integrated to any web page as a DOM element.

<html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> html { height: 100% } body { height: 100%; margin: 0px; padding: 0px }

#map_canvas { height: 100% }</style>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript"> function initialize() { var latlng = new google.maps.LatLng(40.66, 22.95); var myOptions = { zoom: 12, center: latlng,

mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } </script>

</head> <body onload="initialize()"> <div id="map_canvas" style="width:100%; height:100%"></div> </body> </html>

Map canvas DOM element

Map initialization and centering

Library import

Spans the whole page body

Web Information Systems

Page 55: Geographical Data Management for Web Applications

Basic objects

• google.maps.LatLng – Initialization: var latlng = new google.maps.LatLng(lat_p, lon_p);

– Very useful object, e.g. for marker placement.

– In case we don’t know the geo coordinates, but know the street name, we could make use of a Geocoding service.

• google.maps.Map – Initialization: var mapx = new google.maps.Map(domElement,

options);

– Basic parameters: zoom level, center, mapType (ROADMAP, HYBRID, etc.)

– It is possible to have more than one map objects on the same page.

Web Information Systems

Page 56: Geographical Data Management for Web Applications

Zoom level

• Very important to select the proper zoom level depending on the application needs.

zoom = 4 zoom = 8 zoom = 12

zoom = 16 zoom = 18 zoom = 20

Web Information Systems

Page 57: Geographical Data Management for Web Applications

More basic objects

• google.maps.Marker

– Initialization: var latlng = new google.maps.Marker({ position: latlng, map: mapx, title: “POI Title” });

• Very useful object! • Use it for marking Pois.

Web Information Systems

Page 58: Geographical Data Management for Web Applications

Events on the map UI

• Interacting with the map, as in every UI, triggers a variety of events.

• There are two basic event categories:

– User/UI events: Objects on the map respond to certain user actions.

– MVC events: Triggered by changes in the state of the map, e.g. change of value for some parameter (zoom level).

Web Information Systems

Page 59: Geographical Data Management for Web Applications

Map events

• UI events

Look like common DOM events but are separately triggered in order to prevent incompatibility with different browsers.

e.g. the Marker object responds to the following events: – “click”,

– “dblclick”,

– “mouseup”,

– “mousedown”,

– “mouseover”,

– “mouseout”.

Web Information Systems

Page 60: Geographical Data Management for Web Applications

Event listening (I)

• addListener() – Object that “listens” – event that is “listened” – Method that is invoked as soon as the event occurs

• Example:

google.maps.event.addListener(map, 'zoom_changed', function() {

setTimeout(moveToDarwin, 3000); // fire moveToDarwin() after 3 secs

});

google.maps.event.addListener(marker, 'click', function() {

map.setZoom(8);

});

function moveToDarwin() {

var darwin = new google.maps.LatLng(-12.461334, 130.841904);

map.setCenter(darwin);

}

UI event

MVC event

Web Information Systems

Page 61: Geographical Data Management for Web Applications

Event listening (II)

• Argument passing – Only for UI events – Access properties and their values as in any JavaScript object

• Example:

google.maps.event.addListener(map, 'click', function(event) {

placeMarker(event.latLng);

});

function placeMarker(location) {

var marker = new google.maps.Marker({

position: location,

map: map

});

map.setCenter(location);

}

Marker creation at the point clicked by the user.

Map centering on the same point.

Web Information Systems

Page 62: Geographical Data Management for Web Applications

Event listening (IΙI)

• Read and write properties within MVC events – The value that we read may be different than the value at the time of

event triggering (usually we care only for the current value).

• Example:

google.maps.event.addListener(map, 'zoom_changed', function() {

zoomLevel = map.getZoom();

alert("Zoom: " + zoomLevel);

if (zoomLevel == 0) {

map.setZoom(10);

}

});

Web Information Systems

Page 63: Geographical Data Management for Web Applications

Event listening (IV)

• Listen to common DOM events – addDomListener()

• Example: <script>

function initialize() {

// map initialization

} google.maps.event.addDomListener(window, 'load', initialize);

</script>

<body> <div id="map_canvas"></div>

</body>

DOM element window, document.body, etc.

named elements

event name

method to be invoked

Web Information Systems

Page 64: Geographical Data Management for Web Applications

Control elements (I)

• UI elements that enable users to interact with the viewport on the map.

• Navigation control: pan & zoom

• Scale control

• MapType control

Web Information Systems

Page 65: Geographical Data Management for Web Applications

Control elements (II)

• Determine active controls by use of approprate arguments in the map object options (only at create time):

{ navigationControl: boolean, mapTypeControl: boolean, scaleControl: boolean }

• Example:

function initialize() { var myOptions = { zoom: 4, center: new google.maps.LatLng(-33, 151), navigationControl: false, scaleControl: true, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

}

Web Information Systems

Page 66: Geographical Data Management for Web Applications

Control elements (IΙI)

• Control elements offer additional options: – google.maps.NavigationControlStyle.SMALL mini-zoom form – google.maps.NavigationControlStyle.ZOOM_PAN standard form of zoom & pan – google.maps.MapTypeControlStyle.DROPDOWN_MENU select map types from

dropdown menu

• Example:

var myOptions = { ...

mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, ....

} var map = new google.maps.Map(document.getElementById("map_canvas"),

myOptions);

Web Information Systems

Page 67: Geographical Data Management for Web Applications

Control elements (IV)

• Control elements can be placed in different positions on the map:

navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.ZOOM_PAN, position: google.maps.ControlPosition.TOP_RIGHT }

Web Information Systems

Page 68: Geographical Data Management for Web Applications

Control elements (V)

• It is possible to create custom control elements.

• Control elements are essentially div elements.

• In order to create them, you need:

– Define their appearance by use of CSS.

– Determine their interactions by use of event handlers.

– Create div element and add it to controls property of the Map object.

custom control

Web Information Systems

Page 69: Geographical Data Management for Web Applications

Overlays

• Objects with specific coordinates.

InfoWindow Polygons Polylines

Markers Icons

Web Information Systems

Page 70: Geographical Data Management for Web Applications

Overlays - Markers

• Markers are initialized with their position, the map, and a title as arguments (the title appears in a tooltip upon mouse hover).

• It is also possible to animate markers with two predefined animation schemes by use of the method marker.setAnimation(method): – google.maps.Animation.DROP: Drops and stops.

– google.maps.Animation.BOUNCE: Bounces.

– null: Static.

Web Information Systems

Page 71: Geographical Data Management for Web Applications

Overlays - Icons

• Markers with a different look.

• Set the icon property.

• Example: var image = `beachflag.png’;

var myLatLng = new google.maps.LatLng(44.890, 22.27);

var beachMarker = new google.maps.Marker({ position: myLatLng, map: map, icon: image

});

• Options for more complex appearance (e.g. by use of shadow and stacking).

Web Information Systems

Page 72: Geographical Data Management for Web Applications

Polylines

• Useful for presenting routes, directions, etc.

• Example: var flightPlanCoordinates = [

new google.maps.LatLng(37.772323, -122.214897), new google.maps.LatLng(21.291982, -157.821856), new google.maps.LatLng(-18.142599, 178.431), new google.maps.LatLng(-27.46758, 153.027892)

];

var flightPath = new google.maps.Polyline({ path: flightPlanCoordinates, strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 2

});

flightPath.setMap(map);

Web Information Systems

Page 73: Geographical Data Management for Web Applications

Polylines - Processing

• It is possible to dynamically modify a polyline.

• Read and modify the points in a line:

– var path = poly.getPath();

– var latlng_idx = path.getAt(idx) read element on position idx

– path.insertAt(idx) insert element in position idx

– path.removeAt(idx) delete element on position idx

– path.push(latlng) add element at the end of the line

Web Information Systems

Page 74: Geographical Data Management for Web Applications

Polygons

• Polygons cover an area.

• Example: var triangleCoords = [

new google.maps.LatLng(25.774252, -80.190262), new google.maps.LatLng(18.466465, -66.118292), new google.maps.LatLng(32.321384, -64.75737), new google.maps.LatLng(25.774252, -80.190262)

]; bermudaTriangle = new google.maps.Polygon({

paths: triangleCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 2, fillColor: "#FF0000", fillOpacity: 0.3

}); bermudaTriangle.setMap(map);

• Same behaviour as Polylines with respect to processing their elements (e.g. getPath(), etc.).

Same start and end coordinate (optional)

Web Information Systems

Page 75: Geographical Data Management for Web Applications

Info Window (Ι)

• Very important for providing information for a Poi upon click.

• May contain: (a) simple text, (b) HTML code, or (c) DOM element.

• Initialized by use of the following elements:

– content: String (simple text/HTML), DOM node

– position: If the Info Window is to be opened on top of a marker, then the marker position is provided.

– maxWidth: Maximum width in pixels (autowraps text).

Web Information Systems

Page 76: Geographical Data Management for Web Applications

Info Window (ΙΙ)

• Example:

var contentString = '<div id="content">'+ '<div id="siteNotice">'+ '</div>'+

'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+ '<div id="bodyContent">'+ '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' + 'sandstone rock formation in the southern part of the '+ 'Northern Territory, central Australia. It lies 335 km (208 mi) '+ .... + </p>'+ '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">‘ + 'http://en.wikipedia.org/w/index.php?title=Uluru</a> (last visited June 22, 2009).</p>‘ + '</div>‘ + '</div>';

var infowindow = new google.maps.InfoWindow({

content: contentString }); var marker = new google.maps.Marker({

position: myLatlng, map: map, title:"Uluru (Ayers Rock)«

}); google.maps.event.addListener(marker, 'click', function() {

infowindow.open(map,marker); });

Web Information Systems

Page 77: Geographical Data Management for Web Applications

Layers

• Collections of information that are overlaid on top of base map tiles and modify its appearance. They are treated as a single object.

• Existing layers:

– KmlLayer, GeoRSS • Presentation of feed of geotagged posts on a map.

– TrafficLayer

– BicyclingLayer

Web Information Systems

Page 78: Geographical Data Management for Web Applications

Geocoding

• Service for converting between street names and geographical coordinates.

• Rate limit: 2500 calls / day

• Base URL

http://maps.googleapis.com/maps/api/geocode/output?par

output: json / xml

par

required: address OR latlng, sensor

optional: bounds, region, language

Web Information Systems

Page 79: Geographical Data Management for Web Applications

Use Cases: Geographical Data Clustering + Heat Maps

Page 80: Geographical Data Management for Web Applications

Geo-Clustering (1)

• Represent each location p = (lat, lon) as a feature vector: x = [x1 x2]

• Apply some density-based clustering method.

– Prefer methods that do not require setting the number of clusters as parameter (e.g. k-means is not really appropriate). Recommend methods: BIRCH and DBSCAN (they require setting other parameters).

Web Information Systems

Page 81: Geographical Data Management for Web Applications

Geo-Clustering (2)

• Example of using the BIRCH algorithm on a set of geotagged Flickr photos in the vicinity of Thessaloniki.

d = 0.0015 d = 0.0030

CENTER + 40 ΕKKLISIES + ANO POLI

NEA PARALIA

KARABOURNAKI

ARETSOU

Web Information Systems

Page 82: Geographical Data Management for Web Applications

Geo-Clustering (3)

• Example of using clustering in a web application.

http://www.clusttour.gr

markers + polygons + infowindow

Web Information Systems

Page 83: Geographical Data Management for Web Applications

Heat maps

• A visual analytics tool.

• Useful for visualizing location-dependent quantities, e.g. population density, housing prices, crime rates, pollution, etc.

• Requires knowledge or inference of quantity distribution over space.

• Overlay on top of the map.

Web Information Systems

Page 84: Geographical Data Management for Web Applications

• Example: geotagged photos collected from Flickr based on the query “sziget festival”)

Heat maps

GRID

Web Information Systems

Page 85: Geographical Data Management for Web Applications

• Counting frequency in each cell + smoothing based on adjacent cells + color coding

Heat maps

Web Information Systems

Page 86: Geographical Data Management for Web Applications

Hea

t m

aps

(1)

http://flowingdata.com/2011/10/27/language-communities-of-twitter/

Languages in Twitter

Web Information Systems

Page 87: Geographical Data Management for Web Applications

Hea

t m

aps

(2) http://flowingdata.com/2011/12/07/every-death-on-the-

road-in-great-britain/

Deadly car accidents in the UK

Web Information Systems

Page 88: Geographical Data Management for Web Applications

Hea

t m

aps

(3)

http://www.flickr.com/photos/walkingsf/5925793753/in/set-72157627140310742

Versailles

La Grande Arche de la Defense

Orly Airport Twitter (blue) + Flickr (orange)

Web Information Systems

Page 89: Geographical Data Management for Web Applications

Tag

map

s http://tagmaps.research.yahoo.com/worldexplorer.php

London

Rome

Web Information Systems

Page 90: Geographical Data Management for Web Applications

Thank you! Symeon Papadopoulos [email protected]