Category Archives: Code

Kotlin Bites Code

Disclosure: AI-assisted content — consult with an organic-developer.

When building low-level libraries for the JVM — especially those that interact with JNI, rendering engines, or MethodHandles — the exact bytecode emitted matters. Recently I hit a limitation in Kotlin that reminded me why the JVM world still needs Java for certain things.


Where Kotlin Emits Different Bytecode than Java

Here are the main areas where Kotlin’s generated bytecode diverges from Java’s, and why that matters.

AreaJavaKotlinTakeaway
Signature-polymorphic callsEmits correct signature for MethodHandle.invokeExactFalls back to (Object[])Object, causing mismatchesKeep these calls in Java
Default parametersNo defaults → use overloadsGenerates synthetic $default methods with bitmaskAvoid defaults in public APIs for Java clients
Companion objects / @JvmStaticTrue static methodsMethods live in $Companion unless annotatedUse @JvmStatic or plain Java for static APIs
Internal visibilityPackage-private supportedinternal compiles to public + metadataDon’t rely on internal for cross-language encapsulation
SAM interfacesAny functional interface = lambdaOnly fun interface supports SAM; lambdas may create synthetic classesDefine callbacks in Java for performance
NullabilityAll references nullableAnnotations encode nullability, JVM doesn’t enforceExplicit null checks needed in low-level code
Suspend functions / coroutinesN/ACompiles to (Arg, Continuation) → ObjectKeep coroutines in Kotlin wrappers, not core API

Other Kotlin Caveats for Low-Level Code

This wasn’t an isolated issue. Kotlin differs from Java in other ways that make it risky for core interop code:

AreaJavaKotlin limitation
JNI declarationsstatic native boolean render(int, int)Needs @JvmStatic in a companion object; generates synthetic names
JNI header generationjavac -h works directlyNo header generation for Kotlin sources
Checked exceptionsEnforced at compile-timeKotlin ignores them (all unchecked)
Raw typesAllowed (List)Always requires generics (List<*>)
Wildcards? super? extends supportedOnly in / out; cannot express everything
Default paramsNot supported (overloads instead)Compiles to synthetic $default methods
Static membersstatic keywordRequires @JvmStatic in object/companion
Suspend functionsN/ACompiled to Continuation-based state machines, awkward for Java callers

Why This Matters for Library Code

A low-level library often deals with:

  • JNI ↔ JVM bridges
  • OpenGL or native rendering loops
  • Performance-critical calls that must inline
  • Reflection and MethodHandles

All of these require predictable bytecode and signatures. Kotlin often inserts synthetic classes ($Companion$DefaultImpls$WhenMappings) or adapts signatures in ways Java clients (and JNI) do not expect.


Why Keeping the Library Core in Java Makes Sense

BenefitWhy It Matters
One language to maintainSingle codebase, easier contributor onboarding, faster builds
Interop for everyoneJava APIs work in all JVM languages; Kotlin clients lose nothing; Java clients stay safe from Kotlin-only features
JNI friendlinessDirect mapping of Java types to JNI (int → jintboolean → jboolean); javac -h header generation works; avoids $Companion/$DefaultImpls surprises
Bytecode predictabilityNo synthetic baggage ($Companion$default$WhenMappings); avoids mismatched signatures; JIT optimizes exactly as written

Strategy: Java Core + Optional Kotlin API

The pattern I adopted (and which many frameworks use):

  • Core in Java
    • Predictable bytecode
    • JNI header generation
    • Works with MethodHandleVarHandleUnsafe
    • Safe for both Java and Kotlin clients
  • Optional Kotlin extensions (-ktx)
    • Extension functions for ergonomics
    • Coroutines (suspend wrappers)
    • Null-safety
    • DSLs for configuration

This is the same model Android Jetpack follows:
androidx.core in Java, androidx.core-ktx in Kotlin.


Takeaways

PointWhy
MethodHandle supportJava compiler emits exact signatures ((int,int)boolean), Kotlin falls back to (Object[])Object, causing runtime issues
Bytecode predictabilityJava produces direct, predictable bytecode; Kotlin adds synthetic constructs and indirections
JNI compatibilityJava maps directly to JNI types and header generation; Kotlin introduces complications
Best practiceKeep the core in Java for stability and performance; add Kotlin wrappers for ergonomics (DSLs, coroutines, null-safety)

interesting link: https://www.okoone.com/spark/technology-innovation/why-kotlin-swift-and-ruby-are-dropping-off-the-radar/

References

  1. Java SE Docs — MethodHandle and signature-polymorphic methods
    https://docs.oracle.com/javase/8/docs/api/java/lang/invoke/MethodHandle.html
  2. OpenJDK Wiki — Deconstructing MethodHandles
    https://wiki.openjdk.org/display/HotSpot/Deconstructing%2BMethodHandles
  3. Kotlin Docs — Functions and default arguments
    https://kotlinlang.org/docs/functions.html
  4. Medium — Kotlin Function Parameters and Default Values: Behind the Scenes
    https://medium.com/@AlexanderObregon/kotlin-function-parameters-and-default-values-behind-the-scenes-6551fa515fa1
  5. Kotlin Forum — Kotlin bytecode on default parameters
    https://discuss.kotlinlang.org/t/kotlin-bytecode-on-default-parameters/6159
  6. Kotlin Docs — Visibility modifiers (internal, etc.)
    https://www.digitalocean.com/community/tutorials/kotlin-visibility-modifiers-public-protected-internal-private
  7. YouTrack — KT-14416: Support of @PolymorphicSignature in Kotlin compiler
    https://youtrack.jetbrains.com/issue/KT-14416
  8. Baeldung — The @JvmStatic Annotation in Kotlin
    https://www.baeldung.com/kotlin/jvmstatic-annotation
  9. StackOverflow — How to call a static JNI function from Kotlin?
    https://stackoverflow.com/questions/57117201/how-to-call-a-static-jni-function-from-kotlin
  10. The golden age of Kotlin and its uncertain future https://shiftmag.dev/kotlin-vs-java-2392/

SVG fast scaled overlay on Leaflet 1.0 and 0.7

SVGScaled

 update 07/2025: recovered demo here: https://www.sumbera.com/gist/js/leaflet/svg/scaled/SVGScaled.html

SVG can be drawn on map in much more faster way than traditional approaches, at least for points. Traditional approach re-position each element to fit into the view of the map, however SVG is “scalable” so we can use it and it performs much more faster for zoom-in/out.

Few considerations:

  1. SVG itself define viewport by its coordinate space, all outside of this viewport is usually  clipped, so it is important to keep SVG viewport in-line with the viewport of the map. There are approaches that resizes SVG as you zoom-in (here), and while it works, it has a problems in deep-zooms when you need to move on map (actually you move  giant SVG based on the zoom )
  2. translating LatLon to absolute pixel values (like here used for WebGL) is possible solution, however IE and FF has problems with large numbers for transoform (>1 M), So we need to get SVG elements in view coordinates and translate them.
  3. Having some track of bounding box of all elements like again used here should be avoided (SVG or its group element knows about extension of the elements it holds)
  4. So while we keep SVG in the viewport, we need to compensate any shift and zoom by translating <g> (group) of all elements.
  5. So in leaflet when map  moves, SVG is translated back to its original position while <g> is translated forward to reflect the map movement
  6. We need to keep track of LatLon position of either map center or one of the corner – we use topLeft corner.
  7. Leaflet doesn’t do  precise enlargement and rounds view points because of some CSS troubles on some devices (noted here). We need to patch two translating functions in Leaflet to get this right (so SVG enlargement will be aligned with map)… but I need to look on this again, best would be to not patch Leaflet of course.

most important things happen in moveEnd event:

 


 var bounds = this._map.getBounds(); // -- latLng bounds of map viewport
 var topLeftLatLng = new L.LatLng(bounds.getNorth(), bounds.getWest()); // -- topLeft corner of the viewport
 var topLeftLayerPoint = this._map.latLngToLayerPoint(topLeftLatLng); // -- translating to view coord
 var lastLeftLayerPoint = this._map.latLngToLayerPoint(this._lastTopLeftlatLng); 

 var zoom = this._map.getZoom();
 var scaleDelta = this._map.getZoomScale(zoom, this._lastZoom); // -- amount of scale from previous state e.g. 0.5 or 2
 var scaleDiff = this.getScaleDiff(zoom); // -- diff of how far we are from initial scale 

 this._lastZoom = zoom; // -- we need to keep track of last zoom
 var delta = lastLeftLayerPoint.subtract(topLeftLayerPoint); // -- get incremental delta in view coord

 this._lastTopLeftlatLng = topLeftLatLng; // -- we need to keep track of last top left corner, with this we do not need to track center of enlargement
 L.DomUtil.setPosition(this._svg, topLeftLayerPoint); // -- reset svg to keep it inside map viewport

 this._shift._multiplyBy(scaleDelta)._add(delta); // -- compute new relative shift from initial position
 // -- set group element to compensate for svg translation, and scale&amp;lt;/pre&amp;gt;
 this._g.setAttribute(&amp;quot;transform&amp;quot;, &amp;quot;translate(&amp;quot; + this._shift.x + &amp;quot;,&amp;quot; + this._shift.y + &amp;quot;) scale(&amp;quot; + scaleDiff + &amp;quot;)&amp;quot;);


Test page / Gist : http://bl.ocks.org/Sumbera/7e8e57368175a1433791

To better illustrate movement of SVG inside the map, here is a small diagram of basic SVG states:svgpositioning

Drawing Shape File on MapKit

Simple & strightforward test of loading shape file and drawing it on MapKit on iOS8 using drawMapRect

GitHub:https://github.com/Sumbera/SHPonMapKit

 

  • draws only polygons so far
  • primitive optimization, no scale optimisation

Reading of shape file is performed by shapelib

//------------------------------------------------------------
NS_INLINE NSArray *getPolygonsFromShapeFile(NSString *shpFilePath){

    const char *path = [shpFilePath cStringUsingEncoding:NSUTF8StringEncoding];
    SHPHandle shp = SHPOpen(path, &amp;amp;quot;rb&amp;amp;quot;);
    int numEntities;
    int shapeType;

    SHPGetInfo(shp, &amp;amp;amp;numEntities, &amp;amp;amp;shapeType, NULL, NULL);

    NSMutableArray *allPolygons = [[NSMutableArray alloc]init];
    for (int i=0; i&amp;amp;lt;numEntities; i++){
       SHPObject *shpObject = SHPReadObject(shp, i);
       if (shpObject-&amp;amp;gt;nSHPType == SHPT_POLYGON ||
           shpObject-&amp;amp;gt;nSHPType == SHPT_POLYGONZ ||
           shpObject-&amp;amp;gt;nSHPType == SHPT_POLYGONM){

            int numParts = shpObject-&amp;amp;gt;nParts;
            int totalVertexCount = shpObject-&amp;amp;gt;nVertices;

            for (int n=0; n&amp;amp;lt;numParts; n++)
            {
                int startVertex = shpObject-&amp;amp;gt;panPartStart[n];
                int partVertexCount = (n == numParts - 1) ? totalVertexCount - startVertex : shpObject-&amp;amp;gt;panPartStart[n+1] - startVertex;
                int endIndex = startVertex + partVertexCount;

                CLLocationCoordinate2D coords[partVertexCount];
                for (int pv = startVertex, i = 0; pv &amp;amp;lt; endIndex; pv++,i++) {
                    coords[i] =CLLocationCoordinate2DMake(shpObject-&amp;amp;gt;padfY[pv],
                                                          shpObject-&amp;amp;gt;padfX[pv]);
                }
                // -- this actually converts lat lon to mkmappoints projection
                MKPolygon *singlePolygon = [MKPolygon polygonWithCoordinates:coords count:partVertexCount];
                [allPolygons addObject:singlePolygon];
            }
       }

     SHPDestroyObject(shpObject);

  }
    SHPClose(shp);

    return [allPolygons copy];
}

credits/inspiration:

drawing : http://stackoverflow.com/questions/17673410/mkmapview-with-multiple-overlays-memory-issue
parsing : http://www.al-tyus.com/blog/2013/10/14/mapkit-and-esri-shapefiles
shapelib: http://shapelib.maptools.org
dala: http://www.geoportalpraha.cz

 

 

WMS overlay with MapBox-gl-js 0.5.2

alt text

Quick and dirty test of the WMS capabilities of the new MapBox-gl-js 0.5.2 API. First of all, yes ! it is possible to overlay (legacy) WMS over the vector WebGL rendered base map … however the way is not straightforward:

 

  • Needs some ‘hacks’ as current version of the API doesn’t have enough events to supply custom URL before it is loaded. But check latest version of mapbox, it might have better support for this.
  • Another issue is that WMS server has to provide HTTP header with Access-Control-Allow-Origin:* to avoid WebGL CORS failure when loading image (gl.texImage2D). Usually WMS servers don’t care about this, as for normal img tags CORS doesn’t apply. Here WebGL has access to raw image data so WMS provider has to explicitly agree with this.
  • Build process of mapbox-gl-js tend to be as many other large js projects complicated, slow, complex. And specifically on Windows platform it is more difficult to get mapbox-gl-js install and build running then on Mac.

Code is documented to guide you through the process, few highlights:


 // -- rutine originaly found in GlobalMercator.js, simplified
 // -- calculates spherical mercator coordinates from tile coordinates
 function tileBounds(tx, ty, zoom, tileSize) {
    function pixelsToMeters(px, py, zoom) {
     var res = (2 * Math.PI * 6378137 / 256) / Math.pow(2, zoom),
         originShift = 2 * Math.PI * 6378137 / 2,
         x = px * res - originShift,
         y = py * res - originShift;
     return [Math.abs(x), Math.abs(y)];
     };
   var min = pixelsToMeters(tx * tileSize, ty * tileSize, zoom),
         max = pixelsToMeters((tx + 1) * tileSize, (ty + 1) * tileSize, zoom);
return min.concat(max);
}

 
]

// -- save orig _loadTile function so we can call it later
 // -- there was no good pre-load event at mapbox API to get hooked and patch url
// -- we need to use undocumented _loadTile
 var origFunc = sourceObj._loadTile;
    // -- replace _loadTile with own implementation
 sourceObj._loadTile = function (id) {
    // -- we have to patch sourceObj.url, dirty !
    // -- we basically change url on the fly with correct BBOX coordinates
    // -- and leave rest on original _loadTile processing
     var origUrl =sourceObj.tiles[0]
                      .substring(0,sourceObj.tiles[0].indexOf('&amp;amp;amp;BBOX'));
     var origUrl = origUrl +&amp;quot;&amp;amp;amp;BBOX={mleft},{mbottom},{mright},{mtop}&amp;quot;;
     sourceObj.tiles[0] = patchUrl(id, [origUrl]);
     // -- call original method
     return  origFunc.call(sourceObj, id);
 }

 

 

gist available here

demo here (Chrome): https://www.sumbera.com/gist/js/mapbox/index.html

JavaScript – best coding pattern

this or that ? bind or not-bind(this), prototype of prototype ? ehm..all the interesting   things, however better without them in your  code in JavaScript. There is perfect style finally – found in d3, check here: http://bost.ocks.org/mike/chart/ and used in dc.js as well , well described in the d3-cookbok book by Nick Qui Zhu sample code here similar post appeard here “javascript without this”

..it is worth to study the pattern, it will make your code beautiful, modern and readable. You will not need CoffeScript nor TypeScript nor whateverScript.  You even don’t need many other infrastructure or abstractions to get modules or classes  out of JavaScript. It is very elegant.

It is very simple:

function SimpleWidget(spec) {
  var instance = {}; //-- actual instance variable
  var description; // -- private variable

 //-- public API method
instance.foo = function () {
   return instance; //-- returns instance for chaining
 };

 //-- public API method
 instance.boo = function (d) {
   //-- getter of private variable
   if (!arguments.length) return description;

    description = d;  //-- setter of private var
    return instance; //-- returns instance for chaining
 }

 return instance; //-- returns instance for chaining
 }
 // usage
var widget = SimpleWidget({color: &amp;quot;#6495ed&amp;quot;})
            .boo(&amp;quot;argument&amp;quot;)
            .foo();

 

 

 

*** update 1

just came across this great presentation from JSConf.eu : http://2014.jsconf.eu/speakers/sebastian-markbage-minimal-api-surface-area-learning-patterns-instead-of-frameworks.html and this is exactly the way to think about all of the ‘abstracted stuff’ and syntax sugar or salt that is available today for JavaScript.

“It’s much easier to recover from no abstraction than the wrong abstraction.”

*** update 2

NPM and Browserify  is worth to look at, the way how the complex code is done for example in  MapBox-GL-JS . While I don’t like convoluted modules of modules and source maps with concatenated sources, the syntax and modularization is working well.

WebGL polyline tessellation with MapBox-GL-JS

update 07/2025: recovered demo here: Demo here: http://www.sumbera.com/gist/js/mapbox/path/index.html

recovered tesspathy demo here: https://www.sumbera.com/gist/js/tesspathy/tessPathyTest.html

update 09/2015 : test of tesspathy.js library here . Other sources to look:

  1.  http://mattdesl.svbtle.com/drawing-lines-is-hard
  2.  https://github.com/mattdesl/extrude-polyline
  3. https://forum.libcinder.org/topic/smooth-thick-lines-using-geometry-shader

*** original post ***

This post attempted to use pixi.js tessellation of the polyline, this time let’s look on how mapbox-gl-js can do this. In short much more better than pixi.js.

it took slightly more time to get the right routines from mapbox-gl-js and find-out where the tessellation is calculated and drawn. It is actually on two places – in LinBucket.js  and in line shader. FireFox shader editor helped a lot to simplify and extract needed calculations and bring it into the JavaScript (for simplification, note however that shader based approach is the right one as you can influence dynamically thickness of lines, while having precaluclated mesh means each time you need to change thickness of line you have to recalculate whol e mesh and update buffers )

 

// — module require mockups so we can use orig files unmodified
 module = {};
 reqMap = {
‘./elementgroups.js’: ‘ElementGroups’,
‘./buffer.js’ : ‘Buffer’
};
require = function (jsFile) { return eval(reqMap[jsFile]); };

<!-- all mapbox dependency for tesselation of the polyline -->
<script src="http://www.sumbera.com/gist/js/mapbox/pointGeometry.js"></script>
<script src="http://www.sumbera.com/gist/js/mapbox/buffer.js"></script>
<script src="http://www.sumbera.com/gist/js/mapbox/linevertexbuffer.js"></script>
<script src="http://www.sumbera.com/gist/js/mapbox/lineelementbuffer.js"></script>
<script src="http://www.sumbera.com/gist/js/mapbox/elementgroups.js"></script>
<script src="http://www.sumbera.com/gist/js/mapbox/linebucket.js"></script>
<script src="http://www.sumbera.com/gist/data/route.js" charset="utf-8"></script>
 // -- we don't use these buffers, override them later, just set them for addLine func
 var bucket = new LineBucket({}, {
 lineVertex: (LineVertexBuffer.prototype.defaultLength = 16, new LineVertexBuffer()),
 lineElement: (LineElementBuffer.prototype.defaultLength = 16, new LineElementBuffer())
 });

var u_linewidth = { x: 0.00015 };
// override .add to get calculated points
LineVertexBuffer.prototype.add = function (point, extrude, tx, ty, linesofar) {
    point.x = point.x + (u_linewidth.x * LineVertexBuffer.extrudeScale * extrude.x * 0.015873);
    point.y = point.y + (u_linewidth.x * LineVertexBuffer.extrudeScale * extrude.y * 0.015873);
    verts.push( point.x, point.y);
    return this.index;
};

// — pass vertexes into the addLine func that will calculate points
bucket.addLine(rawVerts,“miter”,“butt”,2,1);

prototype  code posted here

WebGL polyline tessellation with pixi.js

update 07/2025: recovered demo here: https://www.sumbera.com/gist/js/pixi/PixiTest.html

update 09/2015  : another triangulation methods (mapbox, tesspathy) mentioned here

pixi.js is a 2D open source library for gaming that includes WebGL support for primitives rendering. Why not to utilize it for polyline renderings on map ? It turned out, however, that the  tesselation of the polylines is not handled well.

most important code snippets:

<script src="Pixi.js"></script>
<script src="Point.js"></script>
<script src="WebGLGraphics.js"></script>

<!-- Data -->
<script src="route.js" charset="utf-8"></script>

<script>
var graphicsData = {
  points: verts,
  lineWidth: 0.00015,
  lineColor: 0x33FF00,
  lineAlpha: 0.8
};

var webGLData = {
  points: [],
  indices: []
};

// -- from pixi/utils
PIXI.hex2rgb = function (hex) {
  return [
    ((hex >> 16) & 0xFF) / 255,
    ((hex >> 8) & 0xFF) / 255,
    (hex & 0xFF) / 255
  ];
};

PIXI.WebGLGraphics.buildLine(graphicsData, webGLData);
</script>

I have put sample here:

OstravaRailways

Another implementaiton of polyline tessellation (seems like more functional) is in mapbox-gl-js  in LineBucket  .Mapbox-gl-js code took quite more time to get it running and debug on Windows platform,I  had to run npm install  from VS command shell and read carefully what all the npm errors are saying (e.g. Python version should be < 3). Then FireFox for some reason haven’t triggered breakpoint on LineBucket.addLine, this took another time to find out that I should debug this rather in Chrome.   See the blog here.Anyway good  experience with all the messy npm modules, their install requirements and unnecessary complexity. Also all the npm modules takes more than 200 MB, but some of them are optional in the install.

After all basic LINE draw in WebGL (without the thicknes and styling) is useful too, as on picture above you can see railways in CZ city Ostrava.

 

WebGL polygons fill with libtess.js

Update 7/2025 : recovered libtess demo here: https://www.sumbera.com/gist/js/libtess/libtessTest.html

recovered earcut demo here: https://www.sumbera.com/gist/js/earcut/earcutTest.html

Update 1.6.2015: geojson-vt seems to do great job in tiling and simplifying polygons. Check this post.

Update 18.1.2015: Vladimir Agafonkin fromMapBox released earcut.js – very fast and reliable triangulation library. Worth to check. Video available here:

Original post:

Brendan Kenny from Google showed  here how he made polygons using libtess.js on Google Maps, so I have tried that too with single large enough polygon on Leaflet with CZ districts.  libtess.js is port from C code . Neither plntri.js (update: see also comments for plntri v2.0 details)  nor PolyK.js were able to triangulate large set  of points as libtess.js.

Update:  I looked on poly2tri.js  too with following results:

I could run 2256 polygons (all together > 3M vertexes)  with poly2tri  16 701 ms  vs 127 834 ms (libtess), however I  had to dirty fix  other various errors from poly2tri (null triangles or “FLIP failed due to missing triangle…so some polygons were wrong..), while  libtess was fine for the  same data.

Here is  a test :  3 M vertexes with 1 M triangles have been by generated by libtess in 127s . poly2tri took 16s.  Drawing is still fine but it is ‘just enough’ for WebGL too.

key part is listed below:

tessy.gluTessNormal(0, 0, 1);
tessy.gluTessBeginPolygon(verts);

tessy.gluTessBeginContour();

data.features[0].geometry.coordinates[0].map(function (d, i) {
pixel = LatLongToPixelXY(d[1], d[0],0);
var coords = [pixel.x, pixel.y, 0];
tessy.gluTessVertex(coords, coords);
});

tessy.gluTessEndContour();
// finish polygon (and time triangulation process)
tessy.gluTessEndPolygon();


code available here: http://bl.ocks.org/sumbera/01cbd44a77b4283e6dcd

There is also EMSCRIPTEN version of the tesslib.c available on github, and I was curious whether this version would increase speed of  computation. I could run it but for large polygons (cca 120 verts of CZ boundary) I had to increase module memory to 64 MB for FireFox.  Tessellata 120T verts in  FF-30 took 21s, IE-11, Ch-36: failed  reporting out of stack memory 🙁

Getting back to version from Brendan  (no emscripten) I quickly measured same data on browsers: IE-11 21s, Ch-36: 31s,  FF-30: 27s .

Update Oct/2014: Polyline tessellation blog here

Leaflet WebGL many points rendering

update 7/’25: recovered demo here: https://www.sumbera.com/gist/js/leaflet/webgl/ManyPointsRendering.html

WebGL is funny – programming in very low level style in JavaScript. This sample plots 86T points using this technology.  .

The code is very straightforward, the only thing is to how points are initially loaded and scaled (instead of reloading each time when map moves).

All points are initially transformed to tile size of 256 x 256 pixels at zoom level 0  and then re-scaled/re-shifted based on the current position of the map. drawingOnCanvas is called from L.CanvasOverlay each time map needs to be drawn (move, zoom)


function drawingOnCanvas(canvasOverlay, params) {
  gl.clear(gl.COLOR_BUFFER_BIT);
  // -- set base matrix to translate canvas pixel coordinates -&amp;amp;amp;amp;amp;amp;gt; webgl coordinates
 mapMatrix.set(pixelsToWebGLMatrix);
  var bounds = leafletMap.getBounds();
  var topLeft = new L.LatLng(bounds.getNorth(), bounds.getWest());
  var offset = LatLongToPixelXY(topLeft.lat, topLeft.lng);
  // -- Scale to current zoom
  var scale = Math.pow(2, leafletMap.getZoom());
 scaleMatrix(mapMatrix, scale, scale);
 translateMatrix(mapMatrix, -offset.x, -offset.y);
  // -- attach matrix value to &amp;amp;amp;amp;#039;mapMatrix&amp;amp;amp;amp;#039; uniform in shader
  gl.uniformMatrix4fv(u_matLoc, false, mapMatrix);
 gl.drawArrays(gl.POINTS, 0, numPoints);
}

More information and insipiration I took from this site

demo here: http://bl.ocks.org/sumbera/c6fed35c377a46ff74c3

For polygons rendering check here and for polyline rendering here

Some good intros to WebGL that might help you to understand the code:

There is a nice intro book to WebGL  WebGL Programming Guide by Kouchi Matsuda and Rodger Lea

To illustrate how variables are passed from JavaScript to shaders used in above example, here are two figures from the book-  figure 5.7 on p. 149, and figure 5.3 on p.144.

This figure shows single buffer (interleaved)that is used fro both coordinates and size. In similar way single buffer is constructed in the example here:

var vertBuffer = gl.createBuffer();
var vertArray = new Float32Array(verts);
var fsize = vertArray.BYTES_PER_ELEMENT;

gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertArray, gl.STATIC_DRAW);
gl.vertexAttribPointer(vertLoc, 2, gl.FLOAT, false,fsize*5,0);
gl.enableVertexAttribArray(vertLoc);
// -- offset for color buffer
gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, fsize*5, fsize*2);
gl.enableVertexAttribArray(colorLoc);

WMS with Google Maps on iOS

Sample for using WMS sources in Google Maps SDK for iOS. available on github here: https://github.com/Sumbera/WMS_iOS_GoogleMapSDK
Provide your API key in the WMSController.h

  • Google Maps for iOS used : 1.7.2 (April 2014)
  • used XCode 5.1.1 (April 2014)
  • iPad Air, iOS 7.1 (should run in iOS6.0 too)

 

There are two ways of overlaying WMS in the Google Maps for iOS SDK:

“Method B”: use GMSTileURLConstructor

   // -- method B. WMS tile layer with GMSTileURLConstructor
      GMSTileURLConstructor urls = 
         ^(NSUInteger x, NSUInteger y, NSUInteger z) {
           BBox bbox = bboxFromXYZ(x,y,z);
           NSString *urlKN = 
             [NSString stringWithFormat:@"Your WMS url&BBOX=%f,%f,%f,%f",
                                           bbox.left,
                                           bbox.bottom,
                                           bbox.right,
                                           bbox.top];

          return [NSURL URLWithString:urlKN];
      };

“Method A”: use custom TileLayer derived from GMSTileLayer

  1. your derived class from GMSTileLayer (here WMSTileLayer.h) will receive tile request
     -(void)requestTileForX:(NSUInteger)x 
    y:(NSUInteger)y
    zoom:(NSUInteger)z
    receiver:(id<GMSTileReceiver>)receiver

  2. WMSTileLayer first checks for cached tile and if found calls :
      [self drawTileAtX:x y:y zoom:z Url:urlStr Receiver:receiver] ;

  3. if tile is not cached we download it, save it to the file system (using MD5 hash) and call to draw it
      [data  writeToFile: filePath  atomically:YES];
    [self drawTileAtX:x y: y zoom: z Url:urlStr Receiver:receiver] ;

  4. drawTileAtX is very simple:
      -(void) drawTileAtX: (NSUInteger) x 
    y:(NSUInteger) y
    zoom:(NSUInteger)zoom
    Url:(NSString*) url
    Receiver: (id<GMSTileReceiver>) receiver {
    UIImage *image = TileLoad(url,NO);
    [receiver receiveTileWithX:x y:y zoom:zoom image:image];
    }

}

both ways are used in this sample.