Skip to content

Conversation

@swilliams-northpointkc
Copy link

Hello,

We at NorthPoint Development recent forked this project for use in an internal project, as we needed a good library for rendering vector tiles to our Google Maps application. During the implementation we made a number of changes we'd like to share in this PR.

At a high level:

  • Changes the project architecture to use ES6 modules, add npm for dependency management, and import the pbf and @mapbox/vector-tile dependencies.
  • Adds jest for unit testing and adds a number of unit tests to avoid regressions in MVTFeature, MVTLayer, and MVTSource.
  • Adds ESLint for code linting, along with standard config.
  • Adds a new build process leveraging vite: relevant code and packages are minified and written to vector-tiles-google-maps/dist when npm run build is executed.
  • Adds JSDoc types and extended documentation.
  • Adds logic to load the Google Maps API key from local storage instead of hardcoding to each example page, and shortcuts to all examples from index.html.
  • Removes some Visual Studio boilerplate files that were not being used.

We would be happy to answer any questions you have about these changes!

swilliams-northpointkc and others added 11 commits January 24, 2023 11:56
* Clean up VSCode-related files

* Add npm project and configs

* Add config files

* Add basic unit test

* Add sourceType of module to eslint configs

* Restore example screenshot
* Clean up VSCode-related files

* Add npm project and configs

* Add config files

* Add basic unit test

* Add sourceType of module to eslint configs

* Restore example screenshot

* Lint files

* Provide module exports from Mercator.js

* Add @googlemaps/jest-mocks package

* Add Jest configs and testing packages

* Add unit tests for Mercator.js

* Add export to MVTFeature

* Add setup script to set global Jest object

* Add unit tests for MVTFeature

* Move some mocks to common-mocks.js and test MVTLayer

* Test MVTSource

* Remove placeholder test
* Started removing protobuf

* Added Vite

* remove default from mvtlayer

* undid previous thing
* Started removing protobuf

* Added Vite

* remove default from mvtlayer

* undid previous thing

* Fixed issue with vector tile features not being parsed

* Removed files

* Fixed issue with MERCATOR

* Removed API key

---------

Co-authored-by: Camden Obertop <camden.obertop@gmail.com>
* updates html files

* fixes ESLint config

* adds google maps types

* updates html files

* rewrite feature and layer complete

* mvtSource rewritten

* updates HTML files

* fixes tests
#7)

* Converted Mercator.js to ES6; added JSDocs documentation

* Added GitHub PR template

* Adjusted wording from numeric to literal to be more clear

* Code review feedback
* Added urlGenerator to MVTSource options

* Added underscore

* Simplified try catch separation
* Added urlGenerator to MVTSource options

* Added underscore

* Simplified try catch separation

* Made changes to url
* Update this repo to reflect bmaps

* deleted files accidentally added
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
deleted file mode 100644
index c4ae61a..0000000
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ /dev/null
@@ -1,28 +0,0 @@
-## Issue Link
-<!-- Be sure to add the issue key in the pull request title so it will automatically transition upon merge. ex. BMAPS-123-->
-[[project acronym]-XXX](https://northpoint-development.atlassian.net/browse/[project acronym]-XXX)
-
-## Description
-<!-- Concisely describe what the pull request does. -->
-
-## Tests
-<!-- Give steps for the reviewer to verify that this PR fixes the problem; or delete the section entirely. -->
-
-## Screenshots
-<!-- Add screenshots to show the problem and the solution; or delete the section entirely. -->
-
-## Checklist
-<!-- Replace  the [ ] with [x] to check the boxes. -->
-ALWAYS
-- [ ] My pull request title matches the following format: [project acronym]-### - [concise title]
-      (Ex. 'BMAPS-123 Updated Pull Request Template')
-- [ ] I added/updated tests for the changes I made. (Or I have explained why I have not)
-- [ ] I have run `npm audit fix` (Maybe multiple times) until all vulnerabilities that can be automatically fixed are fixed. If there are vulnerabilties that will need to be fixed manually, I have created a ticket to do so.
-
-ALMOST ALWAYS
-- [ ] I added or updated documentation/README
-
-SOMETIMES
-- [ ] (If this PR ends in a 0) I have scheduled a team code review
-
-[best_practices]:https://gist.github.com/robertpainsi/b632364184e70900af4ab688decf6f53
\ No newline at end of file
diff --git a/vector-tiles-google-maps/dist/assets/index.js b/vector-tiles-google-maps/dist/assets/index.js
new file mode 100644
index 0000000..a383735
--- /dev/null
+++ b/vector-tiles-google-maps/dist/assets/index.js
@@ -0,0 +1 @@
+(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const r of document.querySelectorAll('link[rel="modulepreload"]'))s(r);new MutationObserver(r=>{for(const n of r)if(n.type==="childList")for(const o of n.addedNodes)o.tagName==="LINK"&&o.rel==="modulepreload"&&s(o)}).observe(document,{childList:!0,subtree:!0});function i(r){const n={};return r.integrity&&(n.integrity=r.integrity),r.referrerpolicy&&(n.referrerPolicy=r.referrerpolicy),r.crossorigin==="use-credentials"?n.credentials="include":r.crossorigin==="anonymous"?n.credentials="omit":n.credentials="same-origin",n}function s(r){if(r.ep)return;r.ep=!0;const n=i(r);fetch(r.href,n)}})();var P={};/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */P.read=function(e,t,i,s,r){var n,o,a=r*8-s-1,h=(1<<a)-1,u=h>>1,l=-7,d=i?r-1:0,f=i?-1:1,x=e[t+d];for(d+=f,n=x&(1<<-l)-1,x>>=-l,l+=a;l>0;n=n*256+e[t+d],d+=f,l-=8);for(o=n&(1<<-l)-1,n>>=-l,l+=s;l>0;o=o*256+e[t+d],d+=f,l-=8);if(n===0)n=1-u;else{if(n===h)return o?NaN:(x?-1:1)*(1/0);o=o+Math.pow(2,s),n=n-u}return(x?-1:1)*o*Math.pow(2,n-s)};P.write=function(e,t,i,s,r,n){var o,a,h,u=n*8-r-1,l=(1<<u)-1,d=l>>1,f=r===23?Math.pow(2,-24)-Math.pow(2,-77):0,x=s?0:n-1,y=s?1:-1,w=t<0||t===0&&1/t<0?1:0;for(t=Math.abs(t),isNaN(t)||t===1/0?(a=isNaN(t)?1:0,o=l):(o=Math.floor(Math.log(t)/Math.LN2),t*(h=Math.pow(2,-o))<1&&(o--,h*=2),o+d>=1?t+=f/h:t+=f*Math.pow(2,1-d),t*h>=2&&(o++,h/=2),o+d>=l?(a=0,o=l):o+d>=1?(a=(t*h-1)*Math.pow(2,r),o=o+d):(a=t*Math.pow(2,d-1)*Math.pow(2,r),o=0));r>=8;e[i+x]=a&255,x+=y,a/=256,r-=8);for(o=o<<r|a,u+=r;u>0;e[i+x]=o&255,x+=y,o/=256,u-=8);e[i+x-y]|=w*128};var O=c,V=P;function c(e){this.buf=ArrayBuffer.isView&&ArrayBuffer.isView(e)?e:new Uint8Array(e||0),this.pos=0,this.type=0,this.length=this.buf.length}c.Varint=0;c.Fixed64=1;c.Bytes=2;c.Fixed32=5;var M=(1<<16)*(1<<16),D=1/M,N=12,C=typeof TextDecoder>"u"?null:new TextDecoder("utf8");c.prototype={destroy:function(){this.buf=null},readFields:function(e,t,i){for(i=i||this.length;this.pos<i;){var s=this.readVarint(),r=s>>3,n=this.pos;this.type=s&7,e(r,t,this),this.pos===n&&this.skip(s)}return t},readMessage:function(e,t){return this.readFields(e,t,this.readVarint()+this.pos)},readFixed32:function(){var e=S(this.buf,this.pos);return this.pos+=4,e},readSFixed32:function(){var e=B(this.buf,this.pos);return this.pos+=4,e},readFixed64:function(){var e=S(this.buf,this.pos)+S(this.buf,this.pos+4)*M;return this.pos+=8,e},readSFixed64:function(){var e=S(this.buf,this.pos)+B(this.buf,this.pos+4)*M;return this.pos+=8,e},readFloat:function(){var e=V.read(this.buf,this.pos,!0,23,4);return this.pos+=4,e},readDouble:function(){var e=V.read(this.buf,this.pos,!0,52,8);return this.pos+=8,e},readVarint:function(e){var t=this.buf,i,s;return s=t[this.pos++],i=s&127,s<128||(s=t[this.pos++],i|=(s&127)<<7,s<128)||(s=t[this.pos++],i|=(s&127)<<14,s<128)||(s=t[this.pos++],i|=(s&127)<<21,s<128)?i:(s=t[this.pos],i|=(s&15)<<28,R(i,e,this))},readVarint64:function(){return this.readVarint(!0)},readSVarint:function(){var e=this.readVarint();return e%2===1?(e+1)/-2:e/2},readBoolean:function(){return Boolean(this.readVarint())},readString:function(){var e=this.readVarint()+this.pos,t=this.pos;return this.pos=e,e-t>=N&&C?tt(this.buf,t,e):Q(this.buf,t,e)},readBytes:function(){var e=this.readVarint()+this.pos,t=this.buf.subarray(this.pos,e);return this.pos=e,t},readPackedVarint:function(e,t){if(this.type!==c.Bytes)return e.push(this.readVarint(t));var i=g(this);for(e=e||[];this.pos<i;)e.push(this.readVarint(t));return e},readPackedSVarint:function(e){if(this.type!==c.Bytes)return e.push(this.readSVarint());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readSVarint());return e},readPackedBoolean:function(e){if(this.type!==c.Bytes)return e.push(this.readBoolean());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readBoolean());return e},readPackedFloat:function(e){if(this.type!==c.Bytes)return e.push(this.readFloat());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readFloat());return e},readPackedDouble:function(e){if(this.type!==c.Bytes)return e.push(this.readDouble());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readDouble());return e},readPackedFixed32:function(e){if(this.type!==c.Bytes)return e.push(this.readFixed32());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readFixed32());return e},readPackedSFixed32:function(e){if(this.type!==c.Bytes)return e.push(this.readSFixed32());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readSFixed32());return e},readPackedFixed64:function(e){if(this.type!==c.Bytes)return e.push(this.readFixed64());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readFixed64());return e},readPackedSFixed64:function(e){if(this.type!==c.Bytes)return e.push(this.readSFixed64());var t=g(this);for(e=e||[];this.pos<t;)e.push(this.readSFixed64());return e},skip:function(e){var t=e&7;if(t===c.Varint)for(;this.buf[this.pos++]>127;);else if(t===c.Bytes)this.pos=this.readVarint()+this.pos;else if(t===c.Fixed32)this.pos+=4;else if(t===c.Fixed64)this.pos+=8;else throw new Error("Unimplemented type: "+t)},writeTag:function(e,t){this.writeVarint(e<<3|t)},realloc:function(e){for(var t=this.length||16;t<this.pos+e;)t*=2;if(t!==this.length){var i=new Uint8Array(t);i.set(this.buf),this.buf=i,this.length=t}},finish:function(){return this.length=this.pos,this.pos=0,this.buf.subarray(0,this.length)},writeFixed32:function(e){this.realloc(4),p(this.buf,e,this.pos),this.pos+=4},writeSFixed32:function(e){this.realloc(4),p(this.buf,e,this.pos),this.pos+=4},writeFixed64:function(e){this.realloc(8),p(this.buf,e&-1,this.pos),p(this.buf,Math.floor(e*D),this.pos+4),this.pos+=8},writeSFixed64:function(e){this.realloc(8),p(this.buf,e&-1,this.pos),p(this.buf,Math.floor(e*D),this.pos+4),this.pos+=8},writeVarint:function(e){if(e=+e||0,e>268435455||e<0){j(e,this);return}this.realloc(4),this.buf[this.pos++]=e&127|(e>127?128:0),!(e<=127)&&(this.buf[this.pos++]=(e>>>=7)&127|(e>127?128:0),!(e<=127)&&(this.buf[this.pos++]=(e>>>=7)&127|(e>127?128:0),!(e<=127)&&(this.buf[this.pos++]=e>>>7&127)))},writeSVarint:function(e){this.writeVarint(e<0?-e*2-1:e*2)},writeBoolean:function(e){this.writeVarint(Boolean(e))},writeString:function(e){e=String(e),this.realloc(e.length*4),this.pos++;var t=this.pos;this.pos=et(this.buf,e,this.pos);var i=this.pos-t;i>=128&&I(t,i,this),this.pos=t-1,this.writeVarint(i),this.pos+=i},writeFloat:function(e){this.realloc(4),V.write(this.buf,e,this.pos,!0,23,4),this.pos+=4},writeDouble:function(e){this.realloc(8),V.write(this.buf,e,this.pos,!0,52,8),this.pos+=8},writeBytes:function(e){var t=e.length;this.writeVarint(t),this.realloc(t);for(var i=0;i<t;i++)this.buf[this.pos++]=e[i]},writeRawMessage:function(e,t){this.pos++;var i=this.pos;e(t,this);var s=this.pos-i;s>=128&&I(i,s,this),this.pos=i-1,this.writeVarint(s),this.pos+=s},writeMessage:function(e,t,i){this.writeTag(e,c.Bytes),this.writeRawMessage(t,i)},writePackedVarint:function(e,t){t.length&&this.writeMessage(e,U,t)},writePackedSVarint:function(e,t){t.length&&this.writeMessage(e,W,t)},writePackedBoolean:function(e,t){t.length&&this.writeMessage(e,$,t)},writePackedFloat:function(e,t){t.length&&this.writeMessage(e,q,t)},writePackedDouble:function(e,t){t.length&&this.writeMessage(e,G,t)},writePackedFixed32:function(e,t){t.length&&this.writeMessage(e,X,t)},writePackedSFixed32:function(e,t){t.length&&this.writeMessage(e,Y,t)},writePackedFixed64:function(e,t){t.length&&this.writeMessage(e,J,t)},writePackedSFixed64:function(e,t){t.length&&this.writeMessage(e,K,t)},writeBytesField:function(e,t){this.writeTag(e,c.Bytes),this.writeBytes(t)},writeFixed32Field:function(e,t){this.writeTag(e,c.Fixed32),this.writeFixed32(t)},writeSFixed32Field:function(e,t){this.writeTag(e,c.Fixed32),this.writeSFixed32(t)},writeFixed64Field:function(e,t){this.writeTag(e,c.Fixed64),this.writeFixed64(t)},writeSFixed64Field:function(e,t){this.writeTag(e,c.Fixed64),this.writeSFixed64(t)},writeVarintField:function(e,t){this.writeTag(e,c.Varint),this.writeVarint(t)},writeSVarintField:function(e,t){this.writeTag(e,c.Varint),this.writeSVarint(t)},writeStringField:function(e,t){this.writeTag(e,c.Bytes),this.writeString(t)},writeFloatField:function(e,t){this.writeTag(e,c.Fixed32),this.writeFloat(t)},writeDoubleField:function(e,t){this.writeTag(e,c.Fixed64),this.writeDouble(t)},writeBooleanField:function(e,t){this.writeVarintField(e,Boolean(t))}};function R(e,t,i){var s=i.buf,r,n;if(n=s[i.pos++],r=(n&112)>>4,n<128||(n=s[i.pos++],r|=(n&127)<<3,n<128)||(n=s[i.pos++],r|=(n&127)<<10,n<128)||(n=s[i.pos++],r|=(n&127)<<17,n<128)||(n=s[i.pos++],r|=(n&127)<<24,n<128)||(n=s[i.pos++],r|=(n&1)<<31,n<128))return _(e,r,t);throw new Error("Expected varint not more than 10 bytes")}function g(e){return e.type===c.Bytes?e.readVarint()+e.pos:e.pos+1}function _(e,t,i){return i?t*4294967296+(e>>>0):(t>>>0)*4294967296+(e>>>0)}function j(e,t){var i,s;if(e>=0?(i=e%4294967296|0,s=e/4294967296|0):(i=~(-e%4294967296),s=~(-e/4294967296),i^4294967295?i=i+1|0:(i=0,s=s+1|0)),e>=18446744073709552e3||e<-18446744073709552e3)throw new Error("Given varint doesn't fit into 10 bytes");t.realloc(10),H(i,s,t),Z(s,t)}function H(e,t,i){i.buf[i.pos++]=e&127|128,e>>>=7,i.buf[i.pos++]=e&127|128,e>>>=7,i.buf[i.pos++]=e&127|128,e>>>=7,i.buf[i.pos++]=e&127|128,e>>>=7,i.buf[i.pos]=e&127}function Z(e,t){var i=(e&7)<<4;t.buf[t.pos++]|=i|((e>>>=3)?128:0),e&&(t.buf[t.pos++]=e&127|((e>>>=7)?128:0),e&&(t.buf[t.pos++]=e&127|((e>>>=7)?128:0),e&&(t.buf[t.pos++]=e&127|((e>>>=7)?128:0),e&&(t.buf[t.pos++]=e&127|((e>>>=7)?128:0),e&&(t.buf[t.pos++]=e&127)))))}function I(e,t,i){var s=t<=16383?1:t<=2097151?2:t<=268435455?3:Math.floor(Math.log(t)/(Math.LN2*7));i.realloc(s);for(var r=i.pos-1;r>=e;r--)i.buf[r+s]=i.buf[r]}function U(e,t){for(var i=0;i<e.length;i++)t.writeVarint(e[i])}function W(e,t){for(var i=0;i<e.length;i++)t.writeSVarint(e[i])}function q(e,t){for(var i=0;i<e.length;i++)t.writeFloat(e[i])}function G(e,t){for(var i=0;i<e.length;i++)t.writeDouble(e[i])}function $(e,t){for(var i=0;i<e.length;i++)t.writeBoolean(e[i])}function X(e,t){for(var i=0;i<e.length;i++)t.writeFixed32(e[i])}function Y(e,t){for(var i=0;i<e.length;i++)t.writeSFixed32(e[i])}function J(e,t){for(var i=0;i<e.length;i++)t.writeFixed64(e[i])}function K(e,t){for(var i=0;i<e.length;i++)t.writeSFixed64(e[i])}function S(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16)+e[t+3]*16777216}function p(e,t,i){e[i]=t,e[i+1]=t>>>8,e[i+2]=t>>>16,e[i+3]=t>>>24}function B(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16)+(e[t+3]<<24)}function Q(e,t,i){for(var s="",r=t;r<i;){var n=e[r],o=null,a=n>239?4:n>223?3:n>191?2:1;if(r+a>i)break;var h,u,l;a===1?n<128&&(o=n):a===2?(h=e[r+1],(h&192)===128&&(o=(n&31)<<6|h&63,o<=127&&(o=null))):a===3?(h=e[r+1],u=e[r+2],(h&192)===128&&(u&192)===128&&(o=(n&15)<<12|(h&63)<<6|u&63,(o<=2047||o>=55296&&o<=57343)&&(o=null))):a===4&&(h=e[r+1],u=e[r+2],l=e[r+3],(h&192)===128&&(u&192)===128&&(l&192)===128&&(o=(n&15)<<18|(h&63)<<12|(u&63)<<6|l&63,(o<=65535||o>=1114112)&&(o=null))),o===null?(o=65533,a=1):o>65535&&(o-=65536,s+=String.fromCharCode(o>>>10&1023|55296),o=56320|o&1023),s+=String.fromCharCode(o),r+=a}return s}function tt(e,t,i){return C.decode(e.subarray(t,i))}function et(e,t,i){for(var s=0,r,n;s<t.length;s++){if(r=t.charCodeAt(s),r>55295&&r<57344)if(n)if(r<56320){e[i++]=239,e[i++]=191,e[i++]=189,n=r;continue}else r=n-55296<<10|r-56320|65536,n=null;else{r>56319||s+1===t.length?(e[i++]=239,e[i++]=191,e[i++]=189):n=r;continue}else n&&(e[i++]=239,e[i++]=191,e[i++]=189,n=null);r<128?e[i++]=r:(r<2048?e[i++]=r>>6|192:(r<65536?e[i++]=r>>12|224:(e[i++]=r>>18|240,e[i++]=r>>12&63|128),e[i++]=r>>6&63|128),e[i++]=r&63|128)}return i}var it=T;function T(e,t){this.x=e,this.y=t}T.prototype={clone:function(){return new T(this.x,this.y)},add:function(e){return this.clone()._add(e)},sub:function(e){return this.clone()._sub(e)},multByPoint:function(e){return this.clone()._multByPoint(e)},divByPoint:function(e){return this.clone()._divByPoint(e)},mult:function(e){return this.clone()._mult(e)},div:function(e){return this.clone()._div(e)},rotate:function(e){return this.clone()._rotate(e)},rotateAround:function(e,t){return this.clone()._rotateAround(e,t)},matMult:function(e){return this.clone()._matMult(e)},unit:function(){return this.clone()._unit()},perp:function(){return this.clone()._perp()},round:function(){return this.clone()._round()},mag:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},equals:function(e){return this.x===e.x&&this.y===e.y},dist:function(e){return Math.sqrt(this.distSqr(e))},distSqr:function(e){var t=e.x-this.x,i=e.y-this.y;return t*t+i*i},angle:function(){return Math.atan2(this.y,this.x)},angleTo:function(e){return Math.atan2(this.y-e.y,this.x-e.x)},angleWith:function(e){return this.angleWithSep(e.x,e.y)},angleWithSep:function(e,t){return Math.atan2(this.x*t-this.y*e,this.x*e+this.y*t)},_matMult:function(e){var t=e[0]*this.x+e[1]*this.y,i=e[2]*this.x+e[3]*this.y;return this.x=t,this.y=i,this},_add:function(e){return this.x+=e.x,this.y+=e.y,this},_sub:function(e){return this.x-=e.x,this.y-=e.y,this},_mult:function(e){return this.x*=e,this.y*=e,this},_div:function(e){return this.x/=e,this.y/=e,this},_multByPoint:function(e){return this.x*=e.x,this.y*=e.y,this},_divByPoint:function(e){return this.x/=e.x,this.y/=e.y,this},_unit:function(){return this._div(this.mag()),this},_perp:function(){var e=this.y;return this.y=this.x,this.x=-e,this},_rotate:function(e){var t=Math.cos(e),i=Math.sin(e),s=t*this.x-i*this.y,r=i*this.x+t*this.y;return this.x=s,this.y=r,this},_rotateAround:function(e,t){var i=Math.cos(e),s=Math.sin(e),r=t.x+i*(this.x-t.x)-s*(this.y-t.y),n=t.y+s*(this.x-t.x)+i*(this.y-t.y);return this.x=r,this.y=n,this},_round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this}};T.convert=function(e){return e instanceof T?e:Array.isArray(e)?new T(e[0],e[1]):e};var st=it,rt=m;function m(e,t,i,s,r){this.properties={},this.extent=i,this.type=0,this._pbf=e,this._geometry=-1,this._keys=s,this._values=r,e.readFields(nt,this,t)}function nt(e,t,i){e==1?t.id=i.readVarint():e==2?ot(i,t):e==3?t.type=i.readVarint():e==4&&(t._geometry=i.pos)}function ot(e,t){for(var i=e.readVarint()+e.pos;e.pos<i;){var s=t._keys[e.readVarint()],r=t._values[e.readVarint()];t.properties[s]=r}}m.types=["Unknown","Point","LineString","Polygon"];m.prototype.loadGeometry=function(){var e=this._pbf;e.pos=this._geometry;for(var t=e.readVarint()+e.pos,i=1,s=0,r=0,n=0,o=[],a;e.pos<t;){if(s<=0){var h=e.readVarint();i=h&7,s=h>>3}if(s--,i===1||i===2)r+=e.readSVarint(),n+=e.readSVarint(),i===1&&(a&&o.push(a),a=[]),a.push(new st(r,n));else if(i===7)a&&a.push(a[0].clone());else throw new Error("unknown command "+i)}return a&&o.push(a),o};m.prototype.bbox=function(){var e=this._pbf;e.pos=this._geometry;for(var t=e.readVarint()+e.pos,i=1,s=0,r=0,n=0,o=1/0,a=-1/0,h=1/0,u=-1/0;e.pos<t;){if(s<=0){var l=e.readVarint();i=l&7,s=l>>3}if(s--,i===1||i===2)r+=e.readSVarint(),n+=e.readSVarint(),r<o&&(o=r),r>a&&(a=r),n<h&&(h=n),n>u&&(u=n);else if(i!==7)throw new Error("unknown command "+i)}return[o,h,a,u]};m.prototype.toGeoJSON=function(e,t,i){var s=this.extent*Math.pow(2,i),r=this.extent*e,n=this.extent*t,o=this.loadGeometry(),a=m.types[this.type],h,u;function l(x){for(var y=0;y<x.length;y++){var w=x[y],F=180-(w.y+n)*360/s;x[y]=[(w.x+r)*360/s-180,360/Math.PI*Math.atan(Math.exp(F*Math.PI/180))-90]}}switch(this.type){case 1:var d=[];for(h=0;h<o.length;h++)d[h]=o[h][0];o=d,l(o);break;case 2:for(h=0;h<o.length;h++)l(o[h]);break;case 3:for(o=at(o),h=0;h<o.length;h++)for(u=0;u<o[h].length;u++)l(o[h][u]);break}o.length===1?o=o[0]:a="Multi"+a;var f={type:"Feature",geometry:{type:a,coordinates:o},properties:this.properties};return"id"in this&&(f.id=this.id),f};function at(e){var t=e.length;if(t<=1)return[e];for(var i=[],s,r,n=0;n<t;n++){var o=ht(e[n]);o!==0&&(r===void 0&&(r=o<0),r===o<0?(s&&i.push(s),s=[e[n]]):s.push(e[n]))}return s&&i.push(s),i}function ht(e){for(var t=0,i=0,s=e.length,r=s-1,n,o;i<s;r=i++)n=e[i],o=e[r],t+=(o.x-n.x)*(n.y+o.y);return t}var lt=rt,ut=E;function E(e,t){this.version=1,this.name=null,this.extent=4096,this.length=0,this._pbf=e,this._keys=[],this._values=[],this._features=[],e.readFields(ct,this,t),this.length=this._features.length}function ct(e,t,i){e===15?t.version=i.readVarint():e===1?t.name=i.readString():e===5?t.extent=i.readVarint():e===2?t._features.push(i.pos):e===3?t._keys.push(i.readString()):e===4&&t._values.push(dt(i))}function dt(e){for(var t=null,i=e.readVarint()+e.pos;e.pos<i;){var s=e.readVarint()>>3;t=s===1?e.readString():s===2?e.readFloat():s===3?e.readDouble():s===4?e.readVarint64():s===5?e.readVarint():s===6?e.readSVarint():s===7?e.readBoolean():null}return t}E.prototype.feature=function(e){if(e<0||e>=this._features.length)throw new Error("feature index out of bounds");this._pbf.pos=this._features[e];var t=this._pbf.readVarint()+this._pbf.pos;return new lt(this._pbf,t,this.extent,this._keys,this._values)};var ft=ut,xt=yt;function yt(e,t){this.layers=e.readFields(wt,{},t)}function wt(e,t,i){if(e===3){var s=new ft(i,i.readVarint()+i.pos);s.length&&(t[s.name]=s)}}var gt=xt;function Ft(e){const t=Math.min(Math.max(Math.sin(e.lat()*(Math.PI/180)),-.9999),.9999);return{x:128+e.lng()*(256/360),y:128+.5*Math.log((1+t)/(1-t))*-(256/(2*Math.PI))}}function b(e){return{lat:(2*Math.atan(Math.exp((e.y-128)/-(256/(2*Math.PI))))-Math.PI/2)/(Math.PI/180),lng:(e.x-128)/(256/360)}}function A(e,t){const s=256/Math.pow(2,t),r=Ft(e);return{x:Math.floor(r.x/s),y:Math.floor(r.y/s),z:t}}function _t(e){e=pt(e);const i=256/Math.pow(2,e.z),s={x:e.x*i,y:e.y*i+i},r={x:e.x*i+i,y:e.y*i};return{sw:b(s),ne:b(r)}}function pt(e){const t=Math.pow(2,e.z);return e.x=(e.x%t+t)%t,e.y=(e.y%t+t)%t,e}function z(e,t){const i=e.getBounds(),s=i.getNorthEast(),r=i.getSouthWest(),n=e.getProjection().fromLatLngToPoint(s),o=e.getProjection().fromLatLngToPoint(r),a=Math.pow(2,e.getZoom()),h=e.getProjection().fromLatLngToPoint(t);return{x:(h.x-o.x)*a,y:(h.y-n.y)*a}}function Tt(e,t){const i=e.getZoom(),s=A(t.latLng,i),r=_t(s),n=new google.maps.LatLng(r.sw),o=new google.maps.LatLng(r.ne),a=z(e,n),h=z(e,o);return{x:t.pixel.x-a.x,y:t.pixel.y-h.y}}function mt(e,t,i,s,r){return Math.pow(e-s,2)+Math.pow(t-r,2)<=Math.pow(i,2)}function vt(e,t){let i=Number.POSITIVE_INFINITY;if(t&&t.length>1)for(let s=0,r=t.length-1;s<r;s++){const n=Vt(e,t[s],t[s+1]);n<=i&&(i=n)}return i}function Vt(e,t,i){const s=e.x,r=e.y,n=t.x,o=t.y,a=i.x,h=i.y,u=s-n,l=r-o,d=a-n,f=h-o,x=u*d+l*f,y=d*d+f*f;let w=-1;y!=0&&(w=x/y);let F,v;w<0?(F=n,v=o):w>1?(F=a,v=h):(F=n+w*d,v=o+w*f);const L=s-F,k=r-v;return Math.sqrt(L*L+k*k)}class St{constructor(t){this.mVTSource=t.mVTSource,this.selected=t.selected,this.featureId=t.featureId,this.tiles=[],this.style=t.style,this.type=t.vectorTileFeature.type,this.properties=t.vectorTileFeature.properties,this.addTileFeature(t.vectorTileFeature,t.tileContext),this._draw=t.customDraw||this.defaultDraw,this.selected&&this.select()}addTileFeature(t,i){this.tiles[i.id]={vectorTileFeature:t,divisor:t.extent/i.tileSize,context2d:!1,paths2d:!1}}getTiles(){return this.tiles}getTile(t){return this.tiles[t.id]}setStyle(t){this.style=t}redrawTiles(){const t=this.mVTSource.map.getZoom();for(const i in this.tiles)this.mVTSource.deleteTileDrawn(i),this.mVTSource.getTileObject(i).zoom==t&&this.mVTSource.redrawTile(i)}toggle(){this.selected?this.deselect():this.select()}select(){this.selected=!0,this.mVTSource.featureSelected(this),this.redrawTiles()}deselect(){this.selected=!1,this.mVTSource.featureDeselected(this),this.redrawTiles()}setSelected(t){this.selected=t}draw(t){const i=this.tiles[t.id];let s=this.style;this.selected&&this.style.selected&&(s=this.style.selected),this._draw(t,i,s,this)}defaultDraw(t,i,s){switch(this.type){case 1:this.drawPoint(t,i,s);break;case 2:this.drawLineString(t,i,s);break;case 3:this.drawPolygon(t,i,s);break}}drawPoint(t,i,s){const r=i.vectorTileFeature.coordinates[0][0],n=this.getPoint(r,t,i.divisor),o=s.radius||3,a=this.getContext2d(t.canvas,s);a.beginPath(),a.arc(n.x,n.y,o,0,Math.PI*2),a.closePath(),a.fill(),a.stroke()}drawLineString(t,i,s){i.context2d=this.getContext2d(t.canvas,s),this.drawCoordinates(t,i),i.context2d.stroke(i.paths2d)}drawPolygon(t,i,s){i.context2d=this.getContext2d(t.canvas,s),this.drawCoordinates(t,i),i.paths2d.closePath(),s.fillStyle&&i.context2d.fill(i.paths2d),s.strokeStyle&&i.context2d.stroke(i.paths2d)}drawCoordinates(t,i){const s=i.vectorTileFeature.coordinates;i.paths2d=new Path2D;for(let r=0,n=s.length;r<n;r++){const o=s[r],a=new Path2D;for(let h=0,u=o.length;h<u;h++){const l=this.getPoint(o[h],t,i.divisor);h==0?a.moveTo(l.x,l.y):a.lineTo(l.x,l.y)}i.paths2d.addPath(a)}}getPaths(t){const i=[],s=this.tiles[t.id],r=s.vectorTileFeature.coordinates;for(let n=0,o=r.length;n<o;n++){const a=[],h=r[n];for(let u=0,l=h.length;u<l;u++){const d=this.getPoint(h[u],t,s.divisor);a.push(d)}a.length>0&&i.push(a)}return i}getContext2d(t,i){const s=t.getContext("2d");for(const r in i)r!=="selected"&&(s[r]=i[r]);return s}getPoint(t,i,s){let r={x:t.x/s,y:t.y/s};return i.parentId&&(r=this._getOverzoomedPoint(r,i)),r}_getOverzoomedPoint(t,i){const s=this.mVTSource.getTileObject(i.parentId),r=this.mVTSource.getTileObject(i.id),n=r.zoom-s.zoom,o=Math.pow(2,n),a=t.x*o,h=t.y*o,u=r.x%o,l=r.y%o;return t.x=a-u*i.tileSize,t.y=h-l*i.tileSize,t}isPointInPath(t,i){const s=this.getTile(i),r=s.context2d,n=s.paths2d;return!r||!n?!1:r.isPointInPath(n,t.x,t.y)}}class Mt{constructor(t){this._lineClickTolerance=2,this._getIDForLayerFeature=t.getIDForLayerFeature,this.style=t.style,this.name=t.name,this._filter=t.filter||!1,this._customDraw=t.customDraw||!1,this._canvasAndMVTFeatures=[],this._mVTFeatures=[]}parseVectorTileFeatures(t,i,s){this._canvasAndMVTFeatures[s.id]={canvas:s.canvas,features:[]};for(let r=0,n=i.length;r<n;r++){const o=i[r];this._parseVectorTileFeature(t,o,s,r)}this.drawTile(s)}_parseVectorTileFeature(t,i,s,r){if(this._filter&&typeof this._filter=="function"&&this._filter(i,s)===!1)return;const n=this.getStyle(i),o=this._getIDForLayerFeature(i)||r;let a=this._mVTFeatures[o];if(a)a.setStyle(n),a.addTileFeature(i,s);else{const h=t.isFeatureSelected(o),u={mVTSource:t,vectorTileFeature:i,tileContext:s,style:n,selected:h,featureId:o,customDraw:this._customDraw};a=new St(u),this._mVTFeatures[o]=a}this._canvasAndMVTFeatures[s.id].features.push(a)}drawTile(t){const i=this._canvasAndMVTFeatures[t.id].features;if(!i)return;const s=[];for(var r=0,n=i.length;r<n;r++){const o=i[r];o.selected?s.push(o):o.draw(t)}for(var r=0,n=s.length;r<n;r++)s[r].draw(t)}getCanvas(t){return this._canvasAndMVTFeatures[t].canvas}getStyle(t){return typeof this.style=="function"?this.style(t):this.style}setStyle(t){this.style=t;for(const i in this._mVTFeatures)this._mVTFeatures[i].setStyle(t)}setSelected(t){this._mVTFeatures[t]!==void 0&&this._mVTFeatures[t].select()}setFilter(t){this._filter=t}handleClickEvent(t,i){const s=this._canvasAndMVTFeatures[t.tileContext.id];if(!s)return t;const r=s.canvas,n=s.features;return!r||!n||(t.feature=this._handleClickEvent(t,n,i)),t}_handleClickEvent(t,i,s){this.selectedFeature=null;const r=t.tileContext.id,n=s.getSelectedFeaturesInTile(r);return this._handleClickFeatures(t,n),this.selectedFeature!=null?this.selectedFeature:(this._handleClickFeatures(t,i),this.selectedFeature!=null?this.selectedFeature:this.selectedFeature)}_handleClickFeatures(t,i){this.minDistance=Number.POSITIVE_INFINITY;for(let s=i.length-1;s>=0;s--){const r=i[s];if(this._handleClickFeature(t,r),this.selectedFeature!=null)return this.selectedFeature}}_handleClickFeature(t,i){switch(i.type){case 3:this._handleClickFeaturePolygon(t,i);break;default:{this._handleClickFeatureDefault(t,i);break}}}_handleClickFeaturePolygon(t,i){i.isPointInPath(t.tilePoint,t.tileContext)&&(this.selectedFeature=i,this.minDistance=0)}_handleClickFeatureDefault(t,i){const s=i.getPaths(t.tileContext);for(let o=s.length-1;o>=0;o--){const a=s[o];switch(i.type){case 1:mt(a[0].x,a[0].y,i.style.radius,t.tilePoint.x,t.tilePoint.y)&&(this.selectedFeature=i,this.minDistance=0);break;case 2:var r=vt(t.tilePoint,a),n=i.selected&&i.style.selected?i.style.selected.lineWidth:i.style.lineWidth;r<n/2+this._lineClickTolerance&&r<this.minDistance&&(this.selectedFeature=i,this.minDistance=r);break}if(this.minDistance==0)return this.selectedFeature}}}class Pt{constructor(t,i){const s=this;this.map=t,this._url=i.url||"",this._sourceMaxZoom=i.sourceMaxZoom||!1,this._debug=i.debug||!1,this.getIDForLayerFeature=i.getIDForLayerFeature||function(r){return r.properties.id||r.properties.Id||r.properties.ID},this._visibleLayers=i.visibleLayers||!1,this._xhrHeaders=i.xhrHeaders||{},this._clickableLayers=i.clickableLayers||!1,this._filter=i.filter||!1,this._cache=i.cache||!1,this._tileSize=i.tileSize||256,this.tileSize=new google.maps.Size(this._tileSize,this._tileSize),this.style=i.style||function(r){const n={};switch(r.type){case 1:n.fillStyle="rgba(49,79,79,1)",n.radius=5,n.selected={fillStyle:"rgba(255,255,0,0.5)",radius:6};break;case 2:n.strokeStyle="rgba(136, 86, 167, 1)",n.lineWidth=3,n.selected={strokeStyle:"rgba(255,25,0,0.5)",lineWidth:4};break;case 3:n.fillStyle="rgba(188, 189, 220, 0.5)",n.strokeStyle="rgba(136, 86, 167, 1)",n.lineWidth=1,n.selected={fillStyle:"rgba(255,140,0,0.3)",strokeStyle:"rgba(255,140,0,1)",lineWidth:2};break}return n},this._customDraw=i.customDraw||!1,this.mVTLayers=[],this._tilesDrawn=[],this._visibleTiles=[],this._selectedFeatures=[],i.selectedFeatures&&this.setSelectedFeatures(i.selectedFeatures),this.map.addListener("zoom_changed",()=>{s._zoomChanged()})}getTile(t,i,s){const r=this.drawTile(t,i,s);return this._setVisibleTile(r),r.canvas}releaseTile(t){}_zoomChanged(){this._resetVisibleTiles(),this._cache||this._resetMVTLayers()}_resetMVTLayers(){this.mVTLayers=[]}_deleteVisibleTile(t){delete this._visibleTiles[t]}_resetVisibleTiles(){this._visibleTiles=[]}_setVisibleTile(t){this._visibleTiles[t.id]=t}drawTile(t,i,s){const r=this.getTileId(i,t.x,t.y);let n=this._tilesDrawn[r];return n||(n=this._createTileContext(t,i,s),this._xhrRequest(n),n)}_createTileContext(t,i,s){const r=this.getTileId(i,t.x,t.y),n=this._createCanvas(s,r),o=this._getParentId(r);return{id:r,canvas:n,zoom:i,tileSize:this._tileSize,parentId:o}}_getParentId(t){let i=!1;if(this._sourceMaxZoom){const s=this.getTileObject(t);if(s.zoom>this._sourceMaxZoom){const r=s.zoom-this._sourceMaxZoom,n=s.zoom-r,o=s.x>>r,a=s.y>>r;i=this.getTileId(n,o,a)}}return i}_createCanvas(t,i){const s=t.createElement("canvas");return s.width=this._tileSize,s.height=this._tileSize,s.id=i,s}getTileId(t,i,s){return[t,i,s].join(":")}getTileObject(t){const i=t.split(":");return{zoom:i[0],x:i[1],y:i[2]}}_xhrRequest(t){const i=this,s=t.parentId||t.id,r=this.getTileObject(s),n=this._url.replace("{z}",r.zoom).replace("{x}",r.x).replace("{y}",r.y),o=new XMLHttpRequest;o.onload=function(){if(o.status=="200"&&o.response)return i._xhrResponseOk(t,o.response);i._drawDebugInfo(t)},o.open("GET",n,!0);for(const a in this._xhrHeaders)o.setRequestHeader(a,this._xhrHeaders[a]);o.responseType="arraybuffer",o.send()}_xhrResponseOk(t,i){if(this.map.getZoom()!=t.zoom)return;const s=new Uint8Array(i),r=new O(s),n=new gt(r);for(var o in n.layers){var a=n.layers[o];a.parsedFeatures=[];for(var h=a._features.length,u=0,l=h;u<l;u++){var d=a.feature(u);d.coordinates=d.loadGeometry(),a.parsedFeatures.push(d)}n.layers[o]=a}this._drawVectorTile(n,t)}_setTileDrawn(t){this._cache&&(this._tilesDrawn[t.id]=t)}deleteTileDrawn(t){delete this._tilesDrawn[t]}_resetTileDrawn(){this._tilesDrawn=[]}_drawVectorTile(t,i){if(this._visibleLayers)for(let n=0,o=this._visibleLayers.length;n<o;n++){var s=this._visibleLayers[n];if(t.layers[s]){var r=t.layers[s];this._drawVectorTileLayer(r,s,i)}}else for(var s in t.layers){var r=t.layers[s];this._drawVectorTileLayer(r,s,i)}i.vectorTile=t,this._drawDebugInfo(i),this._setTileDrawn(i)}_drawVectorTileLayer(t,i,s){this.mVTLayers[i]||(this.mVTLayers[i]=this._createMVTLayer(i)),this.mVTLayers[i].parseVectorTileFeatures(this,t.parsedFeatures,s)}_createMVTLayer(t){const i={getIDForLayerFeature:this.getIDForLayerFeature,filter:this._filter,style:this.style,name:t,customDraw:this._customDraw};return new Mt(i)}_drawDebugInfo(t){if(!this._debug)return;const i=this.getTileObject(t.id),s=this._tileSize,r=this._tileSize,n=t.canvas.getContext("2d");n.strokeStyle="#000000",n.fillStyle="#FFFF00",n.strokeRect(0,0,s,r),n.font="12px Arial",n.fillRect(0,0,5,5),n.fillRect(0,r-5,5,5),n.fillRect(s-5,0,5,5),n.fillRect(s-5,r-5,5,5),n.fillRect(s/2-5,r/2-5,10,10),n.strokeText(t.zoom+" "+i.x+" "+i.y,s/2-30,r/2-10)}onClick(t,i,s){this._multipleSelection=s&&s.multipleSelection||!1,s=this._getMouseOptions(s,!1),this._mouseEvent(t,i,s)}onMouseHover(t,i,s){this._multipleSelection=!1,s=this._getMouseOptions(s,!0),this._mouseEvent(t,i,s)}_getMouseOptions(t,i){return{mouseHover:i,setSelected:t.setSelected||!1,toggleSelection:t.toggleSelection===void 0||t.toggleSelection,limitToFirstVisibleLayer:t.limitToFirstVisibleLayer||!1,delay:t.delay||0}}_mouseEvent(t,i,s){if(!t.pixel||!t.latLng)return;if(s.delay==0)return this._mouseEventContinue(t,i,s);this.event=t;const r=this;setTimeout(function(){t==r.event&&r._mouseEventContinue(r.event,i,s)},s.delay,t)}_mouseEventContinue(t,i,s){i=i||function(){};const r=s.limitToFirstVisibleLayer||!1,n=this.map.getZoom(),o=A(t.latLng,n),a=this.getTileId(o.z,o.x,o.y),h=this._visibleTiles[a];if(!h)return;t.tileContext=h,t.tilePoint=Tt(this.map,t);const u=this._clickableLayers||Object.keys(this.mVTLayers)||[];for(let l=u.length-1;l>=0;l--){const d=u[l],f=this.mVTLayers[d];if(f){var t=f.handleClickEvent(t,this);if(this._mouseSelectedFeature(t,i,s),r&&t.feature)break}}}_mouseSelectedFeature(t,i,s){if(s.setSelected){const r=t.feature;r?s.mouseHover?r.selected||r.select():s.toggleSelection?r.toggle():r.selected||r.select():s.mouseHover&&this.deselectAllFeatures()}i(t)}deselectAllFeatures(){const t=this.map.getZoom(),i=[];for(const s in this._selectedFeatures){const r=this._selectedFeatures[s];if(!r)continue;r.setSelected(!1);const n=r.getTiles();for(const o in n)this.deleteTileDrawn(o),this.getTileObject(o).zoom==t&&(i[o]=!0)}this.redrawTiles(i),this._selectedFeatures=[]}featureSelected(t){this._multipleSelection||this.deselectAllFeatures(),this._selectedFeatures[t.featureId]=t}featureDeselected(t){delete this._selectedFeatures[t.featureId]}setSelectedFeatures(t){t.length>1&&(this._multipleSelection=!0),this.deselectAllFeatures();for(let i=0,s=t.length;i<s;i++){const r=t[i];this._selectedFeatures[r]=!1;for(const n in this.mVTLayers)this.mVTLayers[n].setSelected(r)}}isFeatureSelected(t){return this._selectedFeatures[t]!=null}getSelectedFeatures(){const t=[];for(const i in this._selectedFeatures)t.push(this._selectedFeatures[i]);return t}getSelectedFeaturesInTile(t){const i=[];for(const s in this._selectedFeatures){const r=this._selectedFeatures[s];for(const n in r.tiles)n==t&&i.push(r)}return i}setFilter(t,i){i=i===void 0||i,this._filter=t;for(const s in this.mVTLayers)this.mVTLayers[s].setFilter(t);i&&this.redrawAllTiles()}setStyle(t,i){i=i===void 0||i,this.style=t;for(const s in this.mVTLayers)this.mVTLayers[s].setStyle(t);i&&this.redrawAllTiles()}setVisibleLayers(t,i){i=i===void 0||i,this._visibleLayers=t,i&&this.redrawAllTiles()}getVisibleLayers(){return this._visibleLayers}setClickableLayers(t){this._clickableLayers=t}redrawAllTiles(){this._resetTileDrawn(),this.redrawTiles(this._visibleTiles)}redrawTiles(t){for(const i in t)this.redrawTile(i)}redrawTile(t){this.deleteTileDrawn(t);const i=this._visibleTiles[t];!i||!i.vectorTile||(this.clearTile(i.canvas),this._drawVectorTile(i.vectorTile,i))}clearTile(t){t.getContext("2d").clearRect(0,0,t.width,t.height)}setUrl(t,i){i=i===void 0||i,this._url=t,this._resetMVTLayers(),i&&this.redrawAllTiles()}}window.MVTSource=Pt;
diff --git a/vector-tiles-google-maps/dist/index.html b/vector-tiles-google-maps/dist/index.html
new file mode 100644
index 0000000..789021e
--- /dev/null
+++ b/vector-tiles-google-maps/dist/index.html
@@ -0,0 +1,10 @@
+
+<!DOCTYPE html>
+<html>
+<head>
+  <script type="module" crossorigin src="/assets/index.js"></script>
+</head>
+<body>
+
+</body>
+</html>
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants