Coder Social home page Coder Social logo

offline_geocoder's Introduction

offline_geocoder

An agnostic package to reverse geocode locations from a presupplied geojson file. No internet connection or external services required as your geocoder is generated once ahead of time.

How does it work

This uses dart's build tools to pregenerated source code from a GeoJSON file. The GeoJSON is converted into constant code on compile time wherefore no parsing or loading has to be done at runtime. The package allows you to specify which GeoJSON properties you want to extract and turns them into typed class properties.

Usage

1. Get your source GeoJSON FeatureCollection

You can get public domain licensed geo data from https://github.com/nvkelso/natural-earth-vector like country borders.

Example .geojson file:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "NAME": "Fiji",
        "LEVEL": 2
      },
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [ ... ]
      }
    },
    ...
  ]
}
Loading

2. Create build.yaml config file

Add the build_runner package as a dev_dependency to your pubspec.yaml. Then create a build.yaml file to make use of the offline_geocoder builder.

Example build.yaml

targets:
  $default:
    builders:
      offline_geocoder:
        options:
          # define the location of the GeoJSON file that contains your FeatureCollection
          source_file: 'assets/ne_110m_admin_0_countries.geojson'
          # define the output location of the generated GeoCoder dart file
          output_file: 'lib/services/offline_geocoder.dart'
          # specify which properties you want to extract from the GeoJSON including their data type
          extract_properties: {"NAME": "String", "LEVEL": "int"}

3. Generate and use the GeoCoder

Run dart run build_runner build or dart watch build_runner build from your project directory to generate the GeoCoder class. Import the generated dart file and use the geocoder like this:

final result = GeoCoder.getFromLocation(point);

Example main.dart:

import 'package:latlong2/latlong.dart';
import '/services/offline_geocoder.dart';

void main() {
  final result = GeoCoder.getFromLocation(const LatLng(-18.1248, 178.4501));
  print(result?.name);
  print(result?.level);
}

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.