<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manivannan K on Medium]]></title>
        <description><![CDATA[Stories by Manivannan K on Medium]]></description>
        <link>https://medium.com/@manivannank?source=rss-1de92ec48fcb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aC7UVn_H19dq6DqKMzcROg.jpeg</url>
            <title>Stories by Manivannan K on Medium</title>
            <link>https://medium.com/@manivannank?source=rss-1de92ec48fcb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:26:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manivannank/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[GDAL-Python] Create & save geotiff using gdal in python]]></title>
            <link>https://manivannank.medium.com/gdal-python-create-save-geotiff-using-gdal-in-python-469c2e33cb14?source=rss-1de92ec48fcb------2</link>
            <guid isPermaLink="false">https://medium.com/p/469c2e33cb14</guid>
            <dc:creator><![CDATA[Manivannan K]]></dc:creator>
            <pubDate>Sun, 13 Nov 2022 10:29:03 GMT</pubDate>
            <atom:updated>2022-11-13T10:29:03.547Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, we will see how to create &amp; save an numpy array data as geotiff.</p><ol><li>Import the required packages — gdal, matplotlib &amp; numpy</li></ol><pre>from osgeo import gdal, osr<br>import matplotlib.pyplot as plt<br>import numpy as np</pre><p>2. Let’s create a sample raster data to save it as a geotiff. We have generated an image data with 2 bands and is visualized below.</p><pre>#sample image data<br>data_arr = np.array([[<br>    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],<br>    [0,5,4,0,0,0,5,4,0,0,4,4,3,2,0],<br>    [0,4,0,4,0,4,0,4,0,5,0,0,0,0,0],<br>    [0,3,0,0,3,0,0,3,0,4,0,0,0,0,0],<br>    [0,2,0,0,0,0,0,2,0,4,0,0,0,0,0],<br>    [0,1,0,0,0,0,0,1,0,0,3,2,1,1,0],<br>    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]<br>],<br>[<br>    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],<br>    [0,1,1,0,0,0,1,1,0,0,1,1,1,1,0],<br>    [0,1,0,1,0,1,0,1,0,1,0,0,0,0,0],<br>    [0,1,0,0,1,0,0,1,0,1,0,0,0,0,0],<br>    [0,1,0,0,0,0,0,1,0,1,0,0,0,0,0],<br>    [0,1,0,0,0,0,0,1,0,0,1,1,1,1,0],<br>    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]<br>]])<br>#visualising sample data<br>plt.figure(figsize = (10, 10))<br>plt.imshow(data_arr[0], cmap=&#39;gray&#39;)<br>plt.show()<br>plt.figure(figsize = (10, 10))<br>plt.imshow(data_arr[1], cmap=&#39;gray&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*OTauuocVFYMOzX6rYrLD5A.png" /></figure><p>3. Now, we have to define few parameters — Image geographical extent, resolution, width &amp; height.</p><pre># width &amp; height for any band<br>no_of_bands = data_arr.shape[0]<br>height = data_arr[0].shape[0]<br>width = data_arr[0].shape[1]<br><br><br>#sample geoextent &amp; resolution - Update it according to your requirement<br>x1 = 139.7524088<br>y1 = 35.9806627<br>x2 = 139.7557615<br>y2 = 35.9790435<br>x_res = (x1 - x2)/width<br>y_res = (y1 - y2)/width<br>print(no_of_bands, x1, y1, x2, y2, x_res, y_res, width, height)</pre><p>4. Now that our sample data is ready, let’s start creating a tiff image. First, load the GTiff driver from gdal.</p><pre>driver = gdal.GetDriverByName(&#39;GTiff&#39;)</pre><p>5. Create the target dataset.</p><pre>out_ds = driver.Create(&#39;./output.tif&#39;, width, height, no_of_bands, gdal.GDT_Float32)</pre><p>6. Update the GeoTransform of the dataset.</p><pre>out_ds.SetGeoTransform((x1, x_res, 0, y1, 0, y_res))</pre><p>7. Write data array into dataset bands.</p><pre>for band_no in range(0, no_of_bands):<br>    band = out_ds.GetRasterBand(band_no + 1)<br>    band.WriteArray(data_arr[band_no])<br>    band.FlushCache()</pre><p>8. Update the spatial reference system of the dataset &amp; close the dataset.</p><pre>out_srs = osr.SpatialReference()<br>out_srs.ImportFromEPSG(4326)<br>out_ds.SetProjection(out_srs.ExportToWkt())<br>out_ds = None</pre><p>9. Read &amp; Visualise the data using rasterio.</p><pre># read and visualize the saved geotiff<br>import rasterio<br>from rasterio.plot import show<br>src = rasterio.open(&#39;./output.tif&#39;)<br>fig, ax = plt.subplots(1, figsize=(12, 12))<br>show(src, ax=ax)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*kYm847qWDxIU6cLEt238uA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=469c2e33cb14" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dynamic Shaded Relief map with Leaflet and Cloud Optimized GeoTiff (GEBCO Global Bathymetry) —…]]></title>
            <link>https://manivannank.medium.com/dynamic-shaded-relief-map-with-leaflet-and-cloud-optimized-geotiff-gebco-global-bathymetry-b9012a72f17c?source=rss-1de92ec48fcb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9012a72f17c</guid>
            <category><![CDATA[hillshade]]></category>
            <category><![CDATA[gis]]></category>
            <category><![CDATA[leaflet]]></category>
            <category><![CDATA[geotiff]]></category>
            <category><![CDATA[cloud-optimized-geotiff]]></category>
            <dc:creator><![CDATA[Manivannan K]]></dc:creator>
            <pubDate>Wed, 08 Jun 2022 17:44:06 GMT</pubDate>
            <atom:updated>2022-06-08T18:34:45.469Z</atom:updated>
            <content:encoded><![CDATA[<h3>Dynamic Shaded Relief map with Leaflet and Cloud Optimized GeoTiff (GEBCO Global Bathymetry) — Client-side rendering</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qBL4SRetxvu_eNABP4cPIw.png" /></figure><p>Last weekend, I stumbled upon an interested thought: How can we generate a custom basemap for an web application with terrain/bathymetric details and the flexibility to change styles/colour scheme whenever needed without any tileserver. Demo application: <a href="https://demo.gishorizon.com/shaded-relief/">https://demo.gishorizon.com/shaded-relief/</a></p><p>Usually, we have few options for our basemaps: 1) Generate static tiles (png images) and host it in a http server. 2) Tile server, 3) Use a map service providers like Google, ArcGIS, Mapbox, Openmaptiles. I ruled out Option (3) because of obvious reasons :)</p><p>In option (1), we will have to recreate the tile images, if we want to apply new style/any change in the basemap data and we need a server with considerable amount of storage to host tiles for entire earth. Option (2) requires a server with good computational capacity in the backend. Hmm!?! what if we apply the Option (2) on client side (i.e.) perform the computations/rendering in user’s device :)</p><p>Important part of our process is the power of <strong>Cloud Optimized GeoTiff</strong>. COG is similar to regular geotiff file but it allows parts of the tiff data to be loaded using <strong>HTTP GET range</strong> request. The client application sends the data bytes range (from &amp; to bytes) to the server and receives only the part of the file requested. In our case, this is extremely useful as we only load the data needed for a particular tile to be displayed on the map, considering our elevation data is more than 4GB in size!.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*bWGXqNGWC6ZWWQMS6wIGKg.png" /><figcaption>Example: COG range request</figcaption></figure><p>More info: <a href="https://www.cogeo.org/">https://www.cogeo.org/</a>. You can read more about <a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-1-core-concepts-d2cea7d56b47">TileLayer</a> and <a href="http://wiki.gis.com/wiki/index.php/Shaded_Relief">Shaded Relief</a>.</p><h3>Requirements:</h3><ol><li>GDAL: To generate COG and overviews for the raster data</li><li>Other Client-side libraries: LeafletJs, Proj4Js &amp; GeoTiffJs</li></ol><h3>Data Preparation:</h3><p>We are going to use GEBCO Gridded Bathymetry data for this application. It provides the global coverage of elevation data in meters on a 15 arc-second resolution. It can be downloaded from here: (<a href="https://www.gebco.net/data_and_products/gridded_bathymetry_data/">https://www.gebco.net/data_and_products/gridded_bathymetry_data/</a>)</p><p>Next, we will generate the COG file &amp; the overviews (can be external/internal — here we are generating external overview) using the GDAL commands:</p><pre>gdal_translate original_bathymetry.tiff bathymetry_cog.tiff -co COMPRESS=DEFLATE -co TILED=YES</pre><pre>gdaladdo -ro bathymetry_cog.tif 2 4 8 16 32 64 128 256 --config COMPRESS_OVERVIEW DEFLATE</pre><p>Once done, we can host these in a <a href="https://httpd.apache.org/">http server</a> and get the public url of these files. Optionally, we can upload these files in a GCP/AWS storage bucket and get their public urls. Below are the urls of the files that I have generated (GCP bucket):</p><p>1) <a href="https://storage.googleapis.com/gis-projects-poc-output/bathymetry_cog.tif.ovr">https://storage.googleapis.com/gis-projects-poc-output/bathymetry_cog.tif</a></p><p>2) <a href="https://storage.googleapis.com/gis-projects-poc-output/bathymetry_cog.tif.ovr">https://storage.googleapis.com/gis-projects-poc-output/bathymetry_cog.tif.ovr</a></p><p>That’s it from the data side!!. Uff! There are no backend apis/server :D</p><h3>Tile rendering:</h3><p>In this section, we will be discussing few parts of our client-side tile rendering code. <strong><em>Note:</em></strong> It is not possible to include the entire code in the article. You can check the repository for the <a href="https://github.com/manivannan23k/leaflet-cog-shaded-relief">complete code</a>.</p><p>We will start with creating a custom <em>Leaflet TileLayer</em> by extending the <strong><em>L.GridLayer</em></strong><em> </em>and then, initializing tiff image using GeoTiffJs library with COG &amp; overview urls in the constructor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*YqWs3LCnfMQjDczRckbcdw.png" /></figure><p>The important part is where we override the <strong><em>createTile </em></strong>function of the <em>L.GridLayer</em> to render our shaded relief tile. This funcion returns the <strong><em>canvas image of each tile</em></strong> displayed on the map. We have full control to draw anything on this canvas element. Also, we get the tile coordinates (x, y and zoom level) for the tile to be rendered. This tile coordinates are converted to geographic coordinate system and is used to fetch the required parts of our geotiff data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*iu2SGWiAM3Bm61SplWVr0A.png" /><figcaption>Helper function: Tile coordinate to Geographic coordinate</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/1*YK2KDx-CQTLPgJlFWOceIg.png" /><figcaption>Filtering tiff data using GeoTiffJs</figcaption></figure><p>Once the required data is loaded, we have a 2D array of elevation data for the tile. We loop through each pixel of the canvas (tile) and calculate the shaded relief value from elevation data using the below algorithm (Credit: <a href="https://openlayers.org/en/latest/examples/shaded-relief.html">https://openlayers.org/en/latest/examples/shaded-relief.html</a>). Notice that, the loaded tiff image size may not be same as that of the tile size, so we will have to perform any interpolation technique (here, we use Nearest Neighbour) to get the elevation value for the desired pixel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*9KOF50ub_YN81IoZgFi28g.png" /><figcaption>HillShade Algorithm (Credit: OpenLayers)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*TqP3Cqw_qGpk_e_J6krYtQ.png" /><figcaption>Helper function: Nearest neighbour</figcaption></figure><p>The <strong><em>shaded </em></strong>variable holds the greyscale shaded relief value for the pixel. Additionally, we can apply colours based on the actual elevation to the pixel along with the calculated shade (by applying desired weights to colour and hillshade). We can tweak the Sun Elevation, Azimuth and Vertical exaggeration and see the changes in our map.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*CAj5kG6QCXXmhs8GDAvJtQ.png" /><figcaption>Sample tile</figcaption></figure><p>Complete source code: <a href="https://github.com/manivannan23k/leaflet-cog-shaded-relief">https://github.com/manivannan23k/leaflet-cog-shaded-relief</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9012a72f17c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple MapTileServer using NodeJs & PostGIS — Part 2: Drawing Vector data on canvas]]></title>
            <link>https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-2-drawing-vector-data-on-canvas-32f4a83105e5?source=rss-1de92ec48fcb------2</link>
            <guid isPermaLink="false">https://medium.com/p/32f4a83105e5</guid>
            <category><![CDATA[gis]]></category>
            <category><![CDATA[tileserver]]></category>
            <dc:creator><![CDATA[Manivannan K]]></dc:creator>
            <pubDate>Mon, 22 Mar 2021 17:05:00 GMT</pubDate>
            <atom:updated>2021-03-22T17:45:45.867Z</atom:updated>
            <content:encoded><![CDATA[<h3>Simple MapTileServer using NodeJs &amp; PostGIS — Part 2: Drawing Vector data on canvas</h3><p>In the <a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-1-core-concepts-d2cea7d56b47">Part 1</a>, we have seen the fundamental calculations involved in converting geographic coordinates to pixel coordinates. Now, let’s get started with our server code.</p><h3><strong>Express handler for tile image </strong>request<strong>:</strong></h3><p>Our request handler responds to each tile image request(with params: x, y, z, layerId) from the client side. To get features(in the layer-layerId) that should be rendered in the current tile(x, y, z), we will have to convert the tile coordinates(x, y, z from the request) to LatLng coordinates and get the features within the geometric extent of the tile. The below Js function converts the tile coordinates to LatLng coordinates:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea9655af192e202711cb60febed83891/href">https://medium.com/media/ea9655af192e202711cb60febed83891/href</a></iframe><p>Now, moving to our request handler, we will get <strong>layerId</strong>, <strong>x</strong>, <strong>y</strong> and <strong>z </strong>from the tile image request. Like mentioned above, these params will be used to filter the GIS features in Postgres database. We are using the above mentioned <strong>tileToLatLng()</strong> to find the extent of the tile. This extent will then be used to filter the GIS features using the <strong>ST_Contains </strong>and<strong> ST_Overlaps </strong>PostGIS<strong> </strong>functions.</p><p>The below code snippet shows the express request handler and PostGIS query for filtering the features in the tile. <strong>Note:</strong> We are yet to implement the <strong>canvas.draw() </strong>function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9559344790764badcd6d3cc5a2032ccf/href">https://medium.com/media/9559344790764badcd6d3cc5a2032ccf/href</a></iframe><p>Before going to the canvas rendering/drawing part, check the sample renderer object stored in our firestore db. The <strong>symbol</strong> object will be used by the <strong>canvas.draw() </strong>to get the styling information like color, strokeWidth, etc.,. Please note, different attributes are used by different geometry types.</p><ol><li>Point and MultiPoint — radius, fill, stroke</li><li>LineString &amp; MultiLineString — stroke</li><li>Polygon &amp; MultiPolygon — fill, stroke</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f81eaf192ccd52edb4844d1692c46048/href">https://medium.com/media/f81eaf192ccd52edb4844d1692c46048/href</a></iframe><h3>Drawing GIS data on canvas:</h3><p>Now that, we have covered some basic structure of our database models, let’s go the image rendering part.</p><p>Note that, features with different geometry types have different geometry structure:</p><ol><li>Point — Simplest: a point(lng, lat)</li><li>MultiPoint — Array of points</li><li>LineString — Array of points(path)</li><li>MultiLineString — 2D array of points(array of paths)</li><li>Polygon — array of rings(rings — array of closed path) — 2D array of points</li><li>MultiPolygon — array of polygons(3D array)</li></ol><p>We have to handle all these cases. In the canvas.draw() code snippet(below), we have defined separate functions to handle each case:</p><ul><li>drawPolygon() — Handles MultiPolygon and Polygon type; loops through <strong>rings</strong> in a Polygon/MultiPolygon and calls <strong>drawPath()</strong></li><li>drawPath() — Handles Lines in a MultiLineString or Rings in a Polygon or MultiPolygon; Loops through <strong>each point in the ring/path</strong> and calls <strong>drawPointInPath()</strong></li><li>drawPointInPath() — To handle <strong>any point in a path</strong>(LineString, MultiLineString, Polygon &amp; MultiPolygon); moves to the calculated <strong>pixel coordinates</strong> and adds a line from previous position to new in the canvas.</li><li>drawPoint() — To draw <strong>Point</strong> or <strong>MultiPoint</strong> features</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20b2895ad16e20744068d2986c3b3f96/href">https://medium.com/media/20b2895ad16e20744068d2986c3b3f96/href</a></iframe><p>We have explained<strong> latLngToImagePixel()</strong> and <strong>latLngToTile() </strong>in detail in the <a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-1-core-concepts-d2cea7d56b47">Part 1</a>. If you haven’t checked, please do :)</p><p>You can find a new function <strong>canvasCoordinates() </strong>in the above snipper. We did find the pixel coordinates with the latLngToImagePixel, What is this function doing? Well, our calculated pixel coordinates(not always correct) doesn’t know whether the location occurs inside of the current tile or not. The above function will use the tile coordinates(x, y) from the request to find that. Hope, it’s clear :)</p><p>That’s it!</p><h3>What’s next:</h3><p>What we did, is just a simple version of the tile server using NodeJs. The following can be added to this,</p><ul><li>Caching images for faster tiles</li><li>Custom icon image rendering</li><li>Raster data rendering</li><li>New renderer types: Categories, Classification,..</li></ul><p>If you find any gaps or problems, please do ping me or leave a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32f4a83105e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple MapTileServer using NodeJs & PostGIS — Part 1: Core concepts]]></title>
            <link>https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-1-core-concepts-d2cea7d56b47?source=rss-1de92ec48fcb------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2cea7d56b47</guid>
            <category><![CDATA[gis]]></category>
            <category><![CDATA[tileserver]]></category>
            <dc:creator><![CDATA[Manivannan K]]></dc:creator>
            <pubDate>Mon, 22 Mar 2021 17:03:26 GMT</pubDate>
            <atom:updated>2021-03-22T17:58:03.814Z</atom:updated>
            <content:encoded><![CDATA[<h3>Simple MapTileServer using NodeJs &amp; PostGIS — Part 1: Core concepts</h3><p>You are wondering, Why are we doing this!!? There are many commercial and opensource softwares like ArcGIS Online/Enterprise or Geoserver are available in market to perform this. Hmm…May be you want a customizable, simple, light-weight and non-commercial tileserver or May be, you just want to learn how they work or you wanna do this for fun :). If so, you can build it and this can be a good starting point(regardless of the programming language). In this two-part article, we will see how we can build a simple &amp; lightweight tile server with NodeJs and PostGIS.</p><p>Basically, our node app will be drawing the vector data on a canvas dynamically and serve the image to the client. Be aware that, this is not a production ready implementation of a tileserver. We will not be caching the generated images; Everytime a request comes, our server creates a new image. The article only discuss about the nodejs implementation of the tile image rendering of a GIS vector data with a few PostGIS queries. We will not be discussing project architecture or data models here. Let’s get started!</p><ul><li><a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-1-core-concepts-d2cea7d56b47">Part 1 — Core concepts</a></li><li><a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-2-drawing-vector-data-on-canvas-32f4a83105e5">Part 2 — Drawing Vector data on canvas</a></li></ul><h3>What we will be building,</h3><p>At the end of this article series, we will have a tileserver which renders vector geographical data dynamically and serves them to the client webgis apps using Leaflet or Openlayers.</p><p>It will have service url like this format: <strong>http://{host}/{layer}/{z}/{x}/{y}</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*gS5qHWvP6-N_rrUxomojFg.png" /><figcaption>Eg: http://localhost:8080/tileserver/layer_id/7/92/60</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXVpNspZ8BKA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXVpNspZ8BKA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXVpNspZ8BKA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/66a4e4aa0e37c57624de2e670e0e12c5/href">https://medium.com/media/66a4e4aa0e37c57624de2e670e0e12c5/href</a></iframe><p>Demo url: <a href="https://gishorizon.com/demo.html">https://gishorizon.com/demo.html</a></p><h3>We will be using,</h3><ul><li>NodeJs &amp; ExpressJs environment</li><li>NPM packages: <a href="https://www.npmjs.com/package/canvas">node-canvas</a>, <a href="https://www.npmjs.com/package/pg">node-postgres</a> (Main focus of this article)</li><li>Postgresql database with PostGIS enabled — To store GIS data</li><li>Firebase firestore — To store non-GIS data: layers &amp; styles(not covered in this article)</li></ul><p>Having said that, it’s not mandatory to use any of the above. Each one of the above can be replaced with different with a different approach. For example, instead of Postgresql, one can use file system or Cloud storage bucket to store the GIS data and load &amp; manipulate the data using <a href="https://www.npmjs.com/package/gdal">gdal bindings for NodeJs</a>.</p><h3>Calculations:</h3><p>Before we start writing any code, let’s understand some core concepts behind our server: Coordinate conversion for drawing GIS data on a canvas.</p><p>Check this <a href="https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Pseudo-code">algorithm</a> for converting LatLng coordinates to Tile coordinates and vice versa. The <a href="https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_.28JavaScript.2FActionScript.2C_etc..29">Javascript implementation</a> of the algorthim:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f795d8ec3d7230751e5aa6d328900033/href">https://medium.com/media/f795d8ec3d7230751e5aa6d328900033/href</a></iframe><p>The above code will give the tile coordinates for the given location and zoom level. Let’s check with an example,</p><ul><li><strong>LatLng:</strong> Lat = 11.0, Lng = 79.0, Zoom level = 2</li><li><strong>Output(Tile): </strong>x = 2, y=1</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*k6b4nOiDD7DekoQpBMOBkA.png" /><figcaption>For our example location and zoom, the tile coordinate is (2, 1) as shown above. <a href="https://wiki.openstreetmap.org/wiki/File:Tiled_web_map_numbering.png">Source</a></figcaption></figure><p>Our server will be drawing image for that particular tile coordinates(i.e. It will be drawing that highlighted box). Now, we know where our location is in the tile coordinates and what tile it will be drawing.</p><p>What’s next? We need to identify the pixel coordinates on that tile for our example location. Let’s go back to our <strong>latLngToTile </strong>function. You can notice, we have <strong>Math.floor() </strong>for both coordinates calculation. If you remove that, we will get some value like x = 2.8777, y=1.8771.</p><p>Yup, you are correct!!. The decimal part(0.8777, 0.8771) tells you exactly the position of our example point in the (2, 1)th tile. Now, if we multiply this value with the size of our canvas(tile image size: <strong>256</strong> pixels), we get pixel coordinates (221, 224). The corresponding JS implementation of this algorithm:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42aef95b2498c93a90676910b3fcad03/href">https://medium.com/media/42aef95b2498c93a90676910b3fcad03/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*VPbcYPci9Yp45qZgiJAUUw.png" /></figure><p>The canvas coordinates starts at <strong>top-right</strong> corner (0, 0); x increases as it moves to right; y to the bottom. The above image shows the pixel position of our point on the tile image(256x256 pixels).</p><p>Whether we are rendering raster or vector data, the above calculations are very important. Now that, we have covered the basics, let’s start writing some code.</p><p>If you find any gaps or problems, please do ping me or leave a comment. Will meet you in <a href="https://manivannank.medium.com/simple-maptileserver-using-nodejs-postgis-part-2-drawing-vector-data-on-canvas-32f4a83105e5">Part 2</a>. Thanks:)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2cea7d56b47" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>