Skip to content

Commit

Permalink
Merge pull request #15 from ppKrauss/master
Browse files Browse the repository at this point in the history
cobertura e CLP testados, issue #12
  • Loading branch information
ppKrauss authored Jan 7, 2019
2 parents 138aa4f + 6550069 commit dcf76b4
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 34 deletions.
30 changes: 29 additions & 1 deletion site3/assets/domPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ function drawCell(rmLast=true) {
drawCell_byPoly(Geocode.polygon, $('#fitZoom').is(':checked'), rmLast); //map.js
}

function showCut(p,x,dom_id) {
//OLD!

// no futuro buscar a macrocélula adequada a partir dos dados de cobertura.
//if (!p) p= '030333⬓'; // kx_interno! "6g"b32 = "030333"b4
var res = Geocode.hlp_base4h_cutPrefix(p,x);
if (res) {
var g32 = Geocode.hlp_base4h_to_outBase(res[0],res[1]);
$(dom_id).text(g32);
} else
$(dom_id).text("(inválido)");
}


/**
* Main function to show a cell in the map, by its coordinates.
* NOTE: get cell's level from interface.
Expand Down Expand Up @@ -82,8 +96,18 @@ function setRefPoint(lat, lon) {
$('#dom_base4').text(Geocode.hash_base4h);
$('#dom_geocode').val(showHash);
$('#dom_geocode_digits').html(Geocode.hash.length);

if (COVER.cover_rgxMcl && (t = Geocode.hash_base4h.match(COVER.cover_rgxMcl))) {
//console.log("t=",t," of ",COVER.IdxOf)
if (Geocode.hash_base4h<15) $('#cell_etc').text('(inválido)')
else {
var hash = COVER.name+'-'+COVER.IdxOf[t[1]]+Geocode.hlp_base4h_to_outBase(t[2],t[2].length);
$('#cell_etc').text(hash);
}
} else
$('#cell_etc').text('');
mapCanvas_popup(Geocode.center, showHash);
var tmp = $('#dom_notes').val();
$('#dom_notes').val(tmp+', '+Geocode.hash);
return Geocode.hash;
} else return null;
} // \func
Expand Down Expand Up @@ -115,6 +139,8 @@ function getFromUrl() { // parse URL, get parameters
ret = {geocode:m[1],city:m[2]};
else if ( m = UrlRequest.match(/^#([A-Z][a-z0-9A-Z\-\.\+ ]+)[\/\-:;]+([a-z0-9]+)$/) )
ret = {geocode:m[2],city:m[1]};
else if ( m = UrlRequest.match(/^#([a-z0-9\+ ]+)$/) )
ret = {geocode:m[1]};
return ret;
} // \func

Expand All @@ -136,6 +162,8 @@ function runRequest() {
}
if (r.city) {
console.log("debug1 city ok:",r)
var city = r.city.replace(/^BR\-/i,'').toLowerCase();
cityCanvas.show(city); //,dom_id_ref,dom_class_selected)
used=true;
}
if (!used)
Expand Down
72 changes: 70 additions & 2 deletions site3/assets/lib_Geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ var Geocode = {
,kx_hash_baseAlphabet: '0123456789bcdefghjkmnpqrstuvwxyz' // f(cf_hash_baseAlphabetLabel)
,kx_hash_base: 32 // suffix only! be not confusion with this.dggCell.base of this.dggCell.id
,kx_hash_baseBits:5 // factor to length fill zeros, bits/2
,kx_halfDigit_Detect: /[⬓⬒]$/ // f(cf_halfDigit_0,cf_halfDigit_2)
,kx_halfDigit_Detect: /[⬒-⬓]$/ // f(cf_halfDigit_0,cf_halfDigit_2)
,kx_halfLevel_isValid: true // indica se permitido ou não na precisão.
,kx_latLng_ZERO_digits: 6 // f(cf_latLng_ZERO)
// STATES: see cleanStates() method.

// PLUGS:
,dggCell: null // to use props and call methods of an instance of DGGcell
// minimal DGGcell have .set() and .id.
,cover_rgxMcl:null

// METHODS: set, setWithoutLevel, cloneState, etc.
}; // \obj
Expand Down Expand Up @@ -60,7 +61,6 @@ Geocode.set = function(lngLat, level) {
this.polygon = this.dggCell.polygon; // copy of GeoJSON.

} else if ( !(level % ckb[1]) ) { // UNION OF TWO CELLS:
console.log("... cell by union")
this.center = null;
this.dggCell.set( lngLat, Math.ceil(level) ); // level L+1
var len = this.dggCell.id.length; // number of base4 digits
Expand Down Expand Up @@ -297,6 +297,24 @@ Geocode.hlp_base4_to_outBase = function(x) {
.padStart(outBase_len,'0');
};

Geocode.hlp_base4h_to_outBase = function(xB4h,xB4h_level) {
var good=String(xB4h).trim().match(/^([0-3]+)([⬒-⬓])?$/u); // utf8 must be a range.
if (!good)
throw new Error('ER16: Base4h MUST be string and non-empty');
var lastHalfDigit = good[2]? good[2]: '';
var lastBit=''; // will be '', '0' or '1'.
if (lastHalfDigit) lastBit = (lastBit=='⬒')? '0': '1';
var xB4 = good[1];
if (lastHalfDigit) {
var x_bin = bigInt(xB4,4,'0123').toString(2)+lastBit;
return bigInt(x_bin,2).toString(this.kx_hash_base, this.kx_hash_baseAlphabet)
.padStart(xB4h_level*0.4,'0');
} else
return bigInt(xB4,4,'0123',true)
.toString(this.kx_hash_base, this.kx_hash_baseAlphabet)
.padStart(xB4h_level*0.4,'0'); // usar kx* para 2/5, etc.
};

Geocode.hlp_base4_to_b4half = function(x) {
// lost the last bit changing base4Digit to halfDigit.
x = String(x).replace(/[01]$/,this.cf_halfDigit_0).replace(/[23]$/,this.cf_halfDigit_2);
Expand Down Expand Up @@ -377,6 +395,56 @@ Geocode.hlp_int_to_base4 = function (x,len) {
return x.toString(4).padStart(len,'0');
}; // \func

///

/**
* Converts base4h (with halfBigit) to binary, returning information about original level.
*/
Geocode.hlp_base4h_to_bin = function (xB4h) {
// future kx_regex must use kx_halfDigit_Detect
var good=xB4h.match(/^([0-3]+)([⬒-⬓])?$/u); // utf8 must be a range.
if (!good)
return null;
var lastHalfDigit = good[2]? good[2]: '';
var lastBit=''; // will be '', '0' or '1'.
if (lastHalfDigit) lastBit = (lastBit=='⬒')? '0': '1';
var xB4 = good[1];
var b4_len = xB4.length;
var b2_len = b4_len*2 + ((lastBit==='')?0:1);
var xB2 = (bigInt(xB4,4).toString(2) + lastBit).padStart(b2_len,'0');
return { b2:xB2, level:b4_len+lastHalfDigit?0.5:0 }
}; // \func

/**
* Cut head (prefix) of x in binary string and returns tail as base4j string.
* @param p string base4h input prefix
* @param x string base4h main input
* @return array [base4h tail, float level]
*/
Geocode.hlp_base4h_cutPrefix = function (p,x) {
p = this.hlp_base4h_to_bin(p);
x = this.hlp_base4h_to_bin(x);
if (!p || !x) return null; // error, bad number
if (p.level>x.level) return null; // error, impossible p to be a root
var x_root = x.b2.slice(0,p.b2.length);
if (x_root!=p.b2) return null; //error not same root
var resB2_len = x.b2.length - p.b2.length; // se nao for divisivel por 2 nao pode ser base4, guardar o ultimo bit!
var res = x.b2.slice(p.b2.length);
if (resB2_len!=res.length) console.log("bug5")
var resB4h_len = resB2_len/2.0;
//console.log("res1=",res)
if (Number.isInteger(resB4h_len))
return [bigInt(res,2).toString(4).padStart(resB4h_len,'0'),resB4h_len];
else {
var btoh = {"0":"⬒","1":"⬓"};
var bit = res.slice(-1);
var res = res.slice(0,-1);
//console.log("res2=",res)
var len = Math.floor(resB4h_len);
return [bigInt(res,2).toString(4).padStart(len,'0') + btoh[bit], resB4h_len];
}
}; // \func

// // /// //

/**
Expand Down
19 changes: 19 additions & 0 deletions site3/assets/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ var cityCanvas = {
mapCanvas.fitBounds( cityCanvas.geom.getBounds(), {padding:[70,70], animate:true, duration:1.8} );
});
if (dom_id_ref!==undefined) {

// // // // //
// // COVER!
COVER.name=opt.toUpperCase();
var base='32'; // defaul in the textarea.
var cover = $('#city-PTS-'+opt+" textarea").val().trim().split(/\s*,\s*/);
if (cover[0].charAt(0)==':') base = cover.shift().slice(1);
if (base=='b32') cover = cover.map(
x => Geocode.hlp_outBase_to_base4h(x)
);
COVER.IdxOf = {};
for(var i=0;i<cover.length;i++) {
for ( var j of Geocode.base4h_halfDigitSplit(cover[i]) ) // gambi? relembrar
COVER.IdxOf[j]=i;
}
var rgxOr = cover.map(x=>x.replace('⬒','[01]').replace('⬓','[23]')).join('|');
COVER.cover_rgxMcl = new RegExp('^(' +rgxOr+ ')(.+)$')

// // // // //
// DOM changes, mark city name as selected
var ref_all = dom_id_ref+'-ALL';
var ref_this = dom_id_ref+'-'+opt;
Expand Down
24 changes: 17 additions & 7 deletions site3/geohash-base16.htm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script>

var selectedByUrl = '';
var COVER={};

$(document).ready(function() { // ONLOAD

Expand Down Expand Up @@ -111,9 +112,9 @@
</select>
</p>

<!-- MAPA E SEUS CONTROLES: -->
<div id="mapid" style="width: 100%; height: 400px;"></div>
<script src="assets/map.js"></script>

<p>
Zoom <span id="zoom_val">16</span>:
&nbsp; &nbsp;
Expand All @@ -131,7 +132,7 @@
style="white-space:nowrap;font-family:monospace">geo:<span id="showMouseMove">?</span></span>
</p>

<form><!-- remover tag form -->
<!-- CÉLULA: -->
<fieldset><legend>Célula <b>Geohash com base16</b></legend>
<label style="white-space:nowrap">Geocódigo:
<input type="text" size="18" id="dom_geocode" style="font-weight: bold; color:#008; font-size:135%;"/></label>
Expand All @@ -149,7 +150,7 @@
<option value='14'>L14</option>
<option value='16'>L16 &nbsp;&nbsp; (~500m)</option>
<option value='18'>L18</option>
<option value='20' selected>L20 &nbsp;&nbsp; (~30m)</option>
<option value='20' selected>L20 &nbsp;&nbsp; (~25m)</option>
<option value='22'>L22 &nbsp;&nbsp; (~10m)</option>
<option value='24'>L24</option>
</select></label>
Expand All @@ -161,30 +162,39 @@
&nbsp;&nbsp;&nbsp;
<label id="showBase4" style="white-space:nowrap;">Base4:
<span id="dom_base4" style="font-family:monospace;color:#888"></span></label>
&nbsp;&nbsp;&nbsp;<b>CLP: <span style="color:red" id="cell_etc"></span></b>
</fieldset>

<!-- outr fieldset -->
<!-- CIDADES -->

<div id="city-ALL">
<!-- ["6gwx","6gwz","6gy8","6gy9","6gyb","6gyc","6gyd","6gyf","6gyg","6gz1","6gz4"] -->
<p class="city-LST">Cidade com bordas apresentadas em vermelho:
&nbsp;<a href="javascript:void(0);" onclick="cityCanvas.show('sp-spa','city')" id="city-sp-spa">São Paulo (SP)</a>,
<!-- mostrar amarelo claro ... Cover-set Geohash de "sp-spa" = ['6gwx','6gwz', '6gy8','6gy9','6gyb','6gyc','6gyd','6gyf','6gyg', '6gz1','6gz4'] -->
&nbsp;<a href="javascript:void(0);" onclick="cityCanvas.show('pr-cur','city')" id="city-pr-cur">Curitiba (PR)</a>.
<br/>

</p>
<p class="city-PTS">Pontos de controle na cidade selecionada: <span>(clique em cidade)</span>
<span id="city-PTS-sp-spa" style="display:none">
<a href="javascript:void(0);" onclick="setRefPoint(-23.561618,-46.655996);">MASP</a>,
<a href="javascript:void(0);" onclick="setRefPoint(-23.550375,-46.633937);">Marco-zero-SP</a>, ...
<br/>Cobertura:
<textarea rows="2" cols="64">:b32,6gwx,6gwz,6gy8,6gy9,6gyb,6gyc,6gyd,6gyf,6gyg,6gz1,6gz4</textarea>
</span>
<span id="city-PTS-pr-cur" style="display:none">
<a href="javascript:void(0);" onclick="setRefPoint(-25.48656,-49.2744);">ponto-lixo1</a>,
<a href="javascript:void(0);" onclick="setRefPoint(-25.63145,-49.3595);">ponto-lixo2</a>, ...
<br/>Cobertura:
<textarea rows="2" cols="64">:b32,6gkzv, 6gkzy, 6gkzz, 6gmp8, 6gkzx, 6gkzw, 6gkzt, 6gkzs, 6gkzk, 6gkzm, 6gkzq, 6gkzr, 6gmp2, 6gmp0, 6gkzp, 6gkzn, 6gkzj, 6gkzh, 6gkyu, 6gkyv, 6gkyy, 6gkyz, 6gmn, 6gkyx, 6gkyw, 6gkyt, 6gkyk, 6gkym, 6gkyq, 6gkyr, 6gkyh, 6gkyj</textarea>
</span>
</p>
</div>

</form>

<p>Bloco de notas:<br/>
<textarea id="dom_notes" rows="3" cols="64">
</textarea>
</p>

</body>
</html>
24 changes: 17 additions & 7 deletions site3/geohash-base32ghs.htm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script>

var selectedByUrl = '';
var COVER={};

$(document).ready(function() { // ONLOAD

Expand Down Expand Up @@ -101,9 +102,9 @@
</select>
</p>

<!-- MAPA E SEUS CONTROLES: -->
<div id="mapid" style="width: 100%; height: 400px;"></div>
<script src="assets/map.js"></script>

<p>
Zoom <span id="zoom_val">16</span>:
&nbsp; &nbsp;
Expand All @@ -121,7 +122,7 @@
style="white-space:nowrap;font-family:monospace">geo:<span id="showMouseMove">?</span></span>
</p>

<form><!-- remover tag form -->
<!-- CÉLULA: -->
<fieldset><legend>Célula <b>Geohash padrão </b></legend>
<label style="white-space:nowrap">Geocódigo:
<input type="text" size="15" id="dom_geocode" style="font-weight: bold; color:#008; font-size:135%;"/></label>
Expand All @@ -137,7 +138,7 @@
<option value='12.5'>L12½</option>
<option value='15'>L15 &nbsp;&nbsp; (~1km)</option>
<option value='17.5'>L17½</option>
<option value='20' selected>L20 &nbsp;&nbsp; (~30m)</option>
<option value='20' selected>L20 &nbsp;&nbsp; (~25m)</option>
<option value='22.5'>L22½ &nbsp;&nbsp; (~5m)</option>
<option value='25'>L25 &nbsp;&nbsp; (~1m)</option>
</select></label>
Expand All @@ -149,30 +150,39 @@
&nbsp;&nbsp;&nbsp;
<label id="showBase4" style="white-space:nowrap;">Base4:
<span id="dom_base4" style="font-family:monospace;color:#888"></span></label>
&nbsp;&nbsp;&nbsp;<b>CLP: <span style="color:red" id="cell_etc"></span></b>
</fieldset>

<!-- outr fieldset -->
<!-- CIDADES -->

<div id="city-ALL">
<!-- ["6gwx","6gwz","6gy8","6gy9","6gyb","6gyc","6gyd","6gyf","6gyg","6gz1","6gz4"] -->
<p class="city-LST">Cidade com bordas apresentadas em vermelho:
&nbsp;<a href="javascript:void(0);" onclick="cityCanvas.show('sp-spa','city')" id="city-sp-spa">São Paulo (SP)</a>,
<!-- mostrar amarelo claro ... Cover-set Geohash de "sp-spa" = ['6gwx','6gwz', '6gy8','6gy9','6gyb','6gyc','6gyd','6gyf','6gyg', '6gz1','6gz4'] -->
&nbsp;<a href="javascript:void(0);" onclick="cityCanvas.show('pr-cur','city')" id="city-pr-cur">Curitiba (PR)</a>.
<br/>

</p>
<p class="city-PTS">Pontos de controle na cidade selecionada: <span>(clique em cidade)</span>
<span id="city-PTS-sp-spa" style="display:none">
<a href="javascript:void(0);" onclick="setRefPoint(-23.561618,-46.655996);">MASP</a>,
<a href="javascript:void(0);" onclick="setRefPoint(-23.550375,-46.633937);">Marco-zero-SP</a>, ...
<br/>Cobertura:
<textarea rows="2" cols="64">:b32,6gwx,6gwz,6gy8,6gy9,6gyb,6gyc,6gyd,6gyf,6gyg,6gz1,6gz4</textarea>
</span>
<span id="city-PTS-pr-cur" style="display:none">
<a href="javascript:void(0);" onclick="setRefPoint(-25.48656,-49.2744);">ponto-lixo1</a>,
<a href="javascript:void(0);" onclick="setRefPoint(-25.63145,-49.3595);">ponto-lixo2</a>, ...
<br/>Cobertura:
<textarea rows="2" cols="64">:b32,6gkzv, 6gkzy, 6gkzz, 6gmp8, 6gkzx, 6gkzw, 6gkzt, 6gkzs, 6gkzk, 6gkzm, 6gkzq, 6gkzr, 6gmp2, 6gmp0, 6gkzp, 6gkzn, 6gkzj, 6gkzh, 6gkyu, 6gkyv, 6gkyy, 6gkyz, 6gmn, 6gkyx, 6gkyw, 6gkyt, 6gkyk, 6gkym, 6gkyq, 6gkyr, 6gkyh, 6gkyj</textarea>
</span>
</p>
</div>

</form>

<p>Bloco de notas:<br/>
<textarea id="dom_notes" rows="3" cols="64">
</textarea>
</p>

</body>
</html>
Loading

0 comments on commit dcf76b4

Please sign in to comment.