-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
724 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import webbrowser | ||
|
||
from maplibre import Layer, LayerType, MapOptions | ||
from maplibre.controls import FullscreenControl | ||
from maplibre.ipywidget import MapWidget as Map | ||
from maplibre.sources import GeoJSONSource | ||
|
||
vancouver_blocks = GeoJSONSource( | ||
data="https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/geojson/vancouver-blocks.json", | ||
) | ||
|
||
map_options = MapOptions( | ||
center=(-123.1256, 49.24658), | ||
zoom=12, | ||
hash=True, | ||
pitch=35, | ||
) | ||
|
||
m = Map(map_options) | ||
m.use_message_queue() | ||
m.add_control(FullscreenControl()) | ||
m.add_layer( | ||
Layer( | ||
type=LayerType.LINE, | ||
source=vancouver_blocks, | ||
paint={"line-color": "white"}, | ||
) | ||
) | ||
|
||
temp_file = "/tmp/pymaplibregl.html" | ||
|
||
with open(temp_file, "w") as f: | ||
f.write(m.to_html(style="height: 800px;")) | ||
|
||
webbrowser.open(temp_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<meta charset="UTF-8"> | ||
<title>Pymaplibregl</title> | ||
<meta name="viewport" content="width=device-width,initial-scale=1"> | ||
<script src="https://unpkg.com/maplibre-gl/dist/maplibre-gl.js"></script> | ||
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl/dist/maplibre-gl.css"/> | ||
<body> | ||
<div id="pymaplibregl" style="height: 800px;"></div> | ||
<script> | ||
(()=>{var B=Object.prototype.toString,_=Array.isArray||function(e){return B.call(e)==="[object Array]"};function R(r){return typeof r=="function"}function D(r){return _(r)?"array":typeof r}function P(r){return r.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g,"\\$&")}function j(r,e){return r!=null&&typeof r=="object"&&e in r}function F(r,e){return r!=null&&typeof r!="object"&&r.hasOwnProperty&&r.hasOwnProperty(e)}var H=RegExp.prototype.test;function N(r,e){return H.call(r,e)}var W=/\S/;function q(r){return!N(W,r)}var G={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function K(r){return String(r).replace(/[&<>"'`=\/]/g,function(t){return G[t]})}var V=/\s*/,z=/\s+/,I=/\s*=/,J=/\s*\}/,Q=/#|\^|\/|>|\{|&|=|!/;function X(r,e){if(!r)return[];var t=!1,n=[],a=[],s=[],o=!1,i=!1,p="",u=0;function h(){if(o&&!i)for(;s.length;)delete a[s.pop()];else s=[];o=!1,i=!1}var v,m,x;function U(y){if(typeof y=="string"&&(y=y.split(z,2)),!_(y)||y.length!==2)throw new Error("Invalid tags: "+y);v=new RegExp(P(y[0])+"\\s*"),m=new RegExp("\\s*"+P(y[1])),x=new RegExp("\\s*"+P("}"+y[1]))}U(e||g.tags);for(var l=new E(r),w,f,d,k,L,b;!l.eos();){if(w=l.pos,d=l.scanUntil(v),d)for(var M=0,A=d.length;M<A;++M)k=d.charAt(M),q(k)?(s.push(a.length),p+=k):(i=!0,t=!0,p+=" "),a.push(["text",k,w,w+1]),w+=1,k===` | ||
`&&(h(),p="",u=0,t=!1);if(!l.scan(v))break;if(o=!0,f=l.scan(Q)||"name",l.scan(V),f==="="?(d=l.scanUntil(I),l.scan(I),l.scanUntil(m)):f==="{"?(d=l.scanUntil(x),l.scan(J),l.scanUntil(m),f="&"):d=l.scanUntil(m),!l.scan(m))throw new Error("Unclosed tag at "+l.pos);if(f==">"?L=[f,d,w,l.pos,p,u,t]:L=[f,d,w,l.pos],u++,a.push(L),f==="#"||f==="^")n.push(L);else if(f==="/"){if(b=n.pop(),!b)throw new Error('Unopened section "'+d+'" at '+w);if(b[1]!==d)throw new Error('Unclosed section "'+b[1]+'" at '+w)}else f==="name"||f==="{"||f==="&"?i=!0:f==="="&&U(d)}if(h(),b=n.pop(),b)throw new Error('Unclosed section "'+b[1]+'" at '+l.pos);return Z(Y(a))}function Y(r){for(var e=[],t,n,a=0,s=r.length;a<s;++a)t=r[a],t&&(t[0]==="text"&&n&&n[0]==="text"?(n[1]+=t[1],n[3]=t[3]):(e.push(t),n=t));return e}function Z(r){for(var e=[],t=e,n=[],a,s,o=0,i=r.length;o<i;++o)switch(a=r[o],a[0]){case"#":case"^":t.push(a),n.push(a),t=a[4]=[];break;case"/":s=n.pop(),s[5]=a[2],t=n.length>0?n[n.length-1][4]:e;break;default:t.push(a)}return e}function E(r){this.string=r,this.tail=r,this.pos=0}E.prototype.eos=function(){return this.tail===""};E.prototype.scan=function(e){var t=this.tail.match(e);if(!t||t.index!==0)return"";var n=t[0];return this.tail=this.tail.substring(n.length),this.pos+=n.length,n};E.prototype.scanUntil=function(e){var t=this.tail.search(e),n;switch(t){case-1:n=this.tail,this.tail="";break;case 0:n="";break;default:n=this.tail.substring(0,t),this.tail=this.tail.substring(t)}return this.pos+=n.length,n};function C(r,e){this.view=r,this.cache={".":this.view},this.parent=e}C.prototype.push=function(e){return new C(e,this)};C.prototype.lookup=function(e){var t=this.cache,n;if(t.hasOwnProperty(e))n=t[e];else{for(var a=this,s,o,i,p=!1;a;){if(e.indexOf(".")>0)for(s=a.view,o=e.split("."),i=0;s!=null&&i<o.length;)i===o.length-1&&(p=j(s,o[i])||F(s,o[i])),s=s[o[i++]];else s=a.view[e],p=j(a.view,e);if(p){n=s;break}a=a.parent}t[e]=n}return R(n)&&(n=n.call(this.view)),n};function c(){this.templateCache={_cache:{},set:function(e,t){this._cache[e]=t},get:function(e){return this._cache[e]},clear:function(){this._cache={}}}}c.prototype.clearCache=function(){typeof this.templateCache<"u"&&this.templateCache.clear()};c.prototype.parse=function(e,t){var n=this.templateCache,a=e+":"+(t||g.tags).join(":"),s=typeof n<"u",o=s?n.get(a):void 0;return o==null&&(o=X(e,t),s&&n.set(a,o)),o};c.prototype.render=function(e,t,n,a){var s=this.getConfigTags(a),o=this.parse(e,s),i=t instanceof C?t:new C(t,void 0);return this.renderTokens(o,i,n,e,a)};c.prototype.renderTokens=function(e,t,n,a,s){for(var o="",i,p,u,h=0,v=e.length;h<v;++h)u=void 0,i=e[h],p=i[0],p==="#"?u=this.renderSection(i,t,n,a,s):p==="^"?u=this.renderInverted(i,t,n,a,s):p===">"?u=this.renderPartial(i,t,n,s):p==="&"?u=this.unescapedValue(i,t):p==="name"?u=this.escapedValue(i,t,s):p==="text"&&(u=this.rawValue(i)),u!==void 0&&(o+=u);return o};c.prototype.renderSection=function(e,t,n,a,s){var o=this,i="",p=t.lookup(e[1]);function u(m){return o.render(m,t,n,s)}if(p){if(_(p))for(var h=0,v=p.length;h<v;++h)i+=this.renderTokens(e[4],t.push(p[h]),n,a,s);else if(typeof p=="object"||typeof p=="string"||typeof p=="number")i+=this.renderTokens(e[4],t.push(p),n,a,s);else if(R(p)){if(typeof a!="string")throw new Error("Cannot use higher-order sections without the original template");p=p.call(t.view,a.slice(e[3],e[5]),u),p!=null&&(i+=p)}else i+=this.renderTokens(e[4],t,n,a,s);return i}};c.prototype.renderInverted=function(e,t,n,a,s){var o=t.lookup(e[1]);if(!o||_(o)&&o.length===0)return this.renderTokens(e[4],t,n,a,s)};c.prototype.indentPartial=function(e,t,n){for(var a=t.replace(/[^ \t]/g,""),s=e.split(` | ||
`),o=0;o<s.length;o++)s[o].length&&(o>0||!n)&&(s[o]=a+s[o]);return s.join(` | ||
`)};c.prototype.renderPartial=function(e,t,n,a){if(n){var s=this.getConfigTags(a),o=R(n)?n(e[1]):n[e[1]];if(o!=null){var i=e[6],p=e[5],u=e[4],h=o;p==0&&u&&(h=this.indentPartial(o,u,i));var v=this.parse(h,s);return this.renderTokens(v,t,n,h,a)}}};c.prototype.unescapedValue=function(e,t){var n=t.lookup(e[1]);if(n!=null)return n};c.prototype.escapedValue=function(e,t,n){var a=this.getConfigEscape(n)||g.escape,s=t.lookup(e[1]);if(s!=null)return typeof s=="number"&&a===g.escape?String(s):a(s)};c.prototype.rawValue=function(e){return e[1]};c.prototype.getConfigTags=function(e){return _(e)?e:e&&typeof e=="object"?e.tags:void 0};c.prototype.getConfigEscape=function(e){if(e&&typeof e=="object"&&!_(e))return e.escape};var g={name:"mustache.js",version:"4.2.0",tags:["{{","}}"],clearCache:void 0,escape:void 0,parse:void 0,render:void 0,Scanner:void 0,Context:void 0,Writer:void 0,set templateCache(r){T.templateCache=r},get templateCache(){return T.templateCache}},T=new c;g.clearCache=function(){return T.clearCache()};g.parse=function(e,t){return T.parse(e,t)};g.render=function(e,t,n,a){if(typeof e!="string")throw new TypeError('Invalid template! Template should be a "string" but "'+D(e)+'" was given as the first argument for mustache#render(template, view, partials)');return T.render(e,t,n,a)};g.escape=K;g.Scanner=E;g.Context=C;g.Writer=c;var $=g;function O(r,e,t){return t!==null?$.render(t,r.properties):e===null?Object.keys(r.properties).map(a=>`${a}: ${r.properties[a]}`).join("</br>"):r.properties[e]}var S=class{constructor(e){this._id=e.container,this._map=new maplibregl.Map(e),this._map.on("mouseover",()=>{this._map.getCanvas().style.cursor="pointer"}),this._map.on("mouseout",()=>{this._map.getCanvas().style.cursor=""}),this._map.addControl(new maplibregl.NavigationControl)}getMap(){return this._map}applyMapMethod(e,t){this._map[e](...t)}addControl(e,t,n){this._map.addControl(new maplibregl[e](t),n)}addMarker({lngLat:e,popup:t,options:n}){let a=new maplibregl.Marker(n).setLngLat(e);if(t){let s=new maplibregl.Popup(t.options).setHTML(t.text);a.setPopup(s)}a.addTo(this._map)}addLayer(e){this._map.addLayer(e),typeof Shiny<"u"&&this._map.on("click",e.id,t=>{console.log(t,t.features[0]);let n=e.id.replaceAll("-","_"),a=`${this._id}_layer_${n}`,s={props:t.features[0].properties,layer_id:e.id};console.log(a,s),Shiny.onInputChange(a,s)})}addPopup(e,t=null,n=null){let a={closeButton:!1},s=new maplibregl.Popup(a);this._map.on("click",e,o=>{let i=o.features[0],p=O(i,t,n);s.setLngLat(o.lngLat).setHTML(p).addTo(this._map)})}addTooltip(e,t=null,n=null){let a={closeButton:!1,closeOnClick:!1},s=new maplibregl.Popup(a);this._map.on("mousemove",e,o=>{let i=o.features[0],p=O(i,t,n);s.setLngLat(o.lngLat).setHTML(p).addTo(this._map)}),this._map.on("mouseleave",e,()=>{s.remove()})}setSourceData(e,t){this._map.getSource(e).setData(t)}render(e){e.forEach(([t,n])=>{if(["addLayer","addPopup","addTooltip","addMarker","addPopup","addControl","setSourceData"].includes(t)){console.log("Custom method",t,n),this[t](...n);return}console.log("Map method",t),this.applyMapMethod(t,n)})}};var ee="0.1.0";console.log("pymaplibregl",ee);typeof Shiny>"u"&&(window.pymaplibregl=function({mapOptions:r,calls:e}){let t="pymaplibregl",n=document.getElementById(t),a=new S(Object.assign({container:n.id},r));a.getMap().on("load",()=>{a.render(e)})});if(typeof Shiny<"u"){class r extends Shiny.OutputBinding{find(t){return t.find(".shiny-maplibregl-output")}renderValue(t,n){console.log("id:",t.id,"payload:",n);let a=new S(Object.assign({container:t.id},n.mapData.mapOptions)),s=a.getMap();s.on("load",()=>{a.render(n.mapData.calls)}),s.on("click",i=>{console.log(i);let p=`${t.id}`,u={coords:i.lngLat,point:i.point};console.log(p,u),Shiny.onInputChange(p,u)});let o=`pymaplibregl-${t.id}`;console.log(o),Shiny.addCustomMessageHandler(o,({id:i,calls:p})=>{console.log(i,p),a.render(p)})}}Shiny.outputBindings.register(new r,"shiny-maplibregl-output")}})(); | ||
/*! Bundled license information: | ||
mustache/mustache.mjs: | ||
(*! | ||
* mustache.js - Logic-less {{mustache}} templates with JavaScript | ||
* http://github.com/janl/mustache.js | ||
*) | ||
*/ | ||
|
||
// ... | ||
(() => { | ||
var data = {"mapOptions": {"center": [-74.5447, 40.6892], "style": "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json", "zoom": 8}, "calls": [["addSource", ["wms-test-source", {"tileSize": 256, "tiles": ["https://img.nj.gov/imagerywms/Natural2015?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&transparent=true&width=256&height=256&layers=Natural2015"], "type": "raster"}]], ["addLayer", [{"id": "wms-test-layer", "type": "raster", "source": "wms-test-source"}]]]}; | ||
pymaplibregl(data); | ||
})(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import webbrowser | ||
|
||
from maplibre import Layer, LayerType, Map, MapOptions | ||
from maplibre.sources import RasterTileSource | ||
|
||
SOURCE_ID = "wms-test-source" | ||
LAYER_ID = "wms-test-layer" | ||
|
||
wms_source = RasterTileSource( | ||
tiles=[ | ||
"https://img.nj.gov/imagerywms/Natural2015?bbox={bbox-epsg-3857}&format=image/png&service=WMS&version=1.1.1&request=GetMap&srs=EPSG:3857&transparent=true&width=256&height=256&layers=Natural2015" | ||
], | ||
tile_size=256, | ||
) | ||
wms_layer = Layer(type=LayerType.RASTER, source=SOURCE_ID, id=LAYER_ID) | ||
|
||
map_options = MapOptions(zoom=8, center=(-74.5447, 40.6892)) | ||
|
||
m = Map(map_options=map_options) | ||
m.add_source(SOURCE_ID, wms_source) | ||
m.add_layer(wms_layer) | ||
|
||
temp_file = "/tmp/pymaplibregl.html" | ||
|
||
with open(temp_file, "w") as f: | ||
f.write(m.to_html(style="height: 800px;")) | ||
|
||
webbrowser.open(temp_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- <a href="app.html" target="_blank">See example in action</a> --> | ||
|
||
<iframe src="app.html" height="620px", width="100%" style="border:none;"></iframe> | ||
|
||
```python | ||
-8<-- "wms/app.py" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.