Code Monkey home page Code Monkey logo

swifter-protomaps's Introduction

SwifterProtomaps

Swift package providing methods for serving Protomaps tile databases from httpswift/swifter instances.

Motivation

This package provides a simple ServeProtomapsTiles helper method to serve one or more Protomaps tile databases using HTTP Range header requests, inclusive of setting any necessary CORS headers.

It was designed for use with an iOS application built around WKWebKitView views whose HTML/JavaScript code need to load and render local (on device) Protomaps tiles.

It is not designed to be a general purpose function for serving files using HTTP Range requests.

For a longer version detailing why we did this please see the Serving map tiles to yourself using Protomaps and iOS blog post.

Example

import Swifter
import SwifterProtomaps

do {
            
	guard let root = URL(string: "/path/to/pmtiles") else {
		raise NSException(name:"InvalidURL", reason:"Invalid URL", userInfo:nil).raise()
	}
	
	let port: in_port_t  = 9000
	            
	var opts = ServeProtomapsOptions(root: root)
	opts.AllowOrigins = "*"
	opts.AllowHeaders = "*"
    
	let server = HttpServer()

	server["/pmtiles/:path"] = ServeProtomapsTiles(opts)
	try server.start(port)
	
} catch {
	print("Server start error: \(error)")
}

Note: If you define server inside a function or a closure and don't persist the variable globally the HTTP server will stop as soon as the variable goes out of scope. This is obvious if you stop to think about it but sometimes these things aren't obvious and you end up burning a lot of cycles figuring it out all over again. Maybe it's just me...

And then in your JavaScript code load and use Protomaps as usual, pointing to the server running on localhost:9000:

        const p = new protomaps.PMTiles("http://localhost:9000/pmtiles/example.pmtiles");
        
        p.metadata().then(m => {
            
            let bounds_str = m.bounds.split(',')
            let bounds = [[+bounds_str[1],+bounds_str[0]],[+bounds_str[3],+bounds_str[2]]]
            
            layer = new protomaps.LeafletLayer({
	            attribution: '',
        	    url:p ,
	            bounds: bounds,
            });
            

            layer.addTo(map);
        });
    }

You can see a working example of this in the sfomuseum/swifter-protomaps-example package:

AppTransportSecurity

You will need to ensure your application has the following NSAppTransportSecurity settings:

	<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsLocalNetworking</key>
		<true/>
		<key>NSExceptionDomains</key>
		<dict>
			<key>localhost</key>
			<dict>
				<key>NSExceptionAllowsInsecureHTTPLoads</key>
				<true/>
				<key>NSIncludesSubdomains</key>
				<true/>
			</dict>
		</dict>
	</dict>

Swift Package Manager

Add the following entries to your dependencies block and any relevant target blocks.

dependencies: [
    	.package(url: "https://github.com/sfomuseum/swift-protomaps.git", from: "0.0.1"),
]
.target(
	name: "{YOUR_TARGET}",
	dependencies: [
		.product(name: "SwifterProtomaps", package: "swifter-protomaps")
	]
)

Notes

This package requires:

  • iOS 13.4 or higher
  • MacOS 10.15.4 or higher.

See also

swifter-protomaps's People

Contributors

thisisaaronland avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

swifter-protomaps's Issues

Investigate "Broken pipe" errors writing tile data

For example:

ERROR] 2022-03-15T11:42:36.555-07:00 SwifterProtomaps.swift#L.129 ServeProtomapsTiles(_:) Failed to write body, writeFailed("Broken pipe")

Curiously, it doesn't appear as though insufficient data is being sent to PM. That is, tiles appear to render fine despite these errors.

Is the deferred close method on the open filehandle somehow being triggered prematurely?

https://github.com/sfomuseum/swifter-protomaps/blob/main/Sources/SwifterProtomaps/SwifterProtomaps.swift#L44-L50

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.