-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add support for OSM vector tiles #4518
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.map { | ||
background: #f8f4f0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
layout: example.html | ||
title: OSM Vector Tiles | ||
shortdesc: Using OpenStreetMap vector tiles. | ||
docs: > | ||
A simple vector tiles map with OpenStreetMap vector tiles. **Note**: The tiles used in this example are not optimized for rendering - they clip tiles exactly at the tile boundary instead of adding a buffer, and use geographic coordinates instead of tile relative pixel coordinates in view projection. | ||
tags: "vector, tiles, osm" | ||
--- | ||
<div id="map" class="map"></div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
goog.require('ol.Map'); | ||
goog.require('ol.View'); | ||
goog.require('ol.format.TopoJSON'); | ||
goog.require('ol.layer.VectorTile'); | ||
goog.require('ol.proj'); | ||
goog.require('ol.source.VectorTile'); | ||
goog.require('ol.style.Fill'); | ||
goog.require('ol.style.Stroke'); | ||
goog.require('ol.style.Style'); | ||
|
||
|
||
var format = new ol.format.TopoJSON(); | ||
var tileGrid = ol.tilegrid.createXYZ({ maxZoom: 19 }); | ||
var roadStyleCache = {}; | ||
var roadColor = { | ||
'major_road': '#776', | ||
'minor_road': '#ccb', | ||
'highway': '#f39' | ||
}; | ||
var landuseStyleCache = {}; | ||
var buildingStyle = [ | ||
new ol.style.Style({ | ||
fill: new ol.style.Fill({ | ||
color: '#666', | ||
opacity: 0.4 | ||
}), | ||
stroke: new ol.style.Stroke({ | ||
color: '#444', | ||
width: 1 | ||
}) | ||
}) | ||
]; | ||
|
||
var map = new ol.Map({ | ||
layers: [ | ||
new ol.layer.VectorTile({ | ||
source: new ol.source.VectorTile({ | ||
format: format, | ||
tileGrid: tileGrid, | ||
url: 'http://{a-c}.tile.openstreetmap.us/' + | ||
'vectiles-water-areas/{z}/{x}/{y}.topojson' | ||
}), | ||
style: new ol.style.Style({ | ||
fill: new ol.style.Fill({ | ||
color: '#9db9e8' | ||
}) | ||
}) | ||
}), | ||
new ol.layer.VectorTile({ | ||
source: new ol.source.VectorTile({ | ||
format: format, | ||
tileGrid: tileGrid, | ||
url: 'http://{a-c}.tile.openstreetmap.us/' + | ||
'vectiles-highroad/{z}/{x}/{y}.topojson' | ||
}), | ||
style: function(feature, resolution) { | ||
var kind = feature.get('kind'); | ||
var railway = feature.get('railway'); | ||
var sort_key = feature.get('sort_key'); | ||
var styleKey = kind + '/' + railway + '/' + sort_key; | ||
var style = roadStyleCache[styleKey]; | ||
if (!style) { | ||
var color, width; | ||
if (railway) { | ||
color = '#7de'; | ||
width = 1; | ||
} else { | ||
color = roadColor[kind]; | ||
width = kind == 'highway' ? 1.5 : 1; | ||
} | ||
style = new ol.style.Style({ | ||
stroke: new ol.style.Stroke({ | ||
color: color, | ||
width: width | ||
}), | ||
zIndex: sort_key | ||
}); | ||
roadStyleCache[styleKey] = style; | ||
} | ||
return style; | ||
} | ||
}), | ||
new ol.layer.VectorTile({ | ||
source: new ol.source.VectorTile({ | ||
format: format, | ||
tileGrid: tileGrid, | ||
url: 'http://{a-c}.tile.openstreetmap.us/' + | ||
'vectiles-buildings/{z}/{x}/{y}.topojson' | ||
}), | ||
style: function(f, resolution) { | ||
return (resolution < 10) ? buildingStyle : null; | ||
} | ||
}), | ||
new ol.layer.VectorTile({ | ||
source: new ol.source.VectorTile({ | ||
format: format, | ||
tileGrid: tileGrid, | ||
url: 'http://{a-c}.tile.openstreetmap.us/' + | ||
'vectiles-land-usages/{z}/{x}/{y}.topojson' | ||
}), | ||
visible: false, | ||
style: function(feature, resolution) { | ||
var kind = feature.get('kind'); | ||
var styleKey = kind; | ||
var style = landuseStyleCache[styleKey]; | ||
if (!style) { | ||
var color, width; | ||
color = { | ||
'parking': '#ddd', | ||
'industrial': '#aaa', | ||
'urban area': '#aaa', | ||
'park': '#76C759', | ||
'school': '#DA10E7', | ||
'garden': '#76C759', | ||
'pitch': '#D58F8D', | ||
'scrub': '#3E7D28', | ||
'residential': '#4C9ED9' | ||
}[kind]; | ||
width = kind == 'highway' ? 1.5 : 1; | ||
style = new ol.style.Style({ | ||
stroke: new ol.style.Stroke({ | ||
color: color, | ||
width: width | ||
}), | ||
fill: new ol.style.Fill({ | ||
color: color, | ||
opacity: 0.5 | ||
}) | ||
}); | ||
landuseStyleCache[styleKey] = style; | ||
} | ||
return style; | ||
} | ||
}) | ||
], | ||
target: 'map', | ||
view: new ol.View({ | ||
center: ol.proj.fromLonLat([-74.0064, 40.7142]), | ||
maxZoom: 19, | ||
zoom: 15 | ||
}) | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -677,12 +677,14 @@ ol.proj.get = function(projectionLike) { | |
ol.proj.equivalent = function(projection1, projection2) { | ||
if (projection1 === projection2) { | ||
return true; | ||
} else if (projection1.getCode() === projection2.getCode()) { | ||
return projection1.getUnits() === projection2.getUnits(); | ||
} | ||
var equalUnits = projection1.getUnits() === projection2.getUnits(); | ||
if (projection1.getCode() === projection2.getCode()) { | ||
return equalUnits; | ||
} else { | ||
var transformFn = ol.proj.getTransformFromProjections( | ||
projection1, projection2); | ||
return transformFn === ol.proj.cloneTransform; | ||
return transformFn === ol.proj.cloneTransform && equalUnits; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this line ever return true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good then. I was just wondering. |
||
} | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference, this could also just be a
ol.style.Style
instance instead of an array (and you get a very slight performance gain for never using arrays).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, one I forgot to change. Thanks, I created #4535.