geo_utilities 0.0.1 copy "geo_utilities: ^0.0.1" to clipboard
geo_utilities: ^0.0.1 copied to clipboard

A comprehensive Dart package for geospatial and geometric calculations, suitable for Flutter and server-side applications.

example/lib/example.dart

// ignore_for_file: avoid_print

import 'package:geo_utilities/src/geo_utilities.dart';
import 'constants/constants.dart';
import 'utils/distanceToCenterPointInVector.dart';

void main() {
  // Instantiate the Geo class to access its utility methods.
  final geo = GeoUtilities();

  // --- 1. Polygon Area ---
  print('1. Calculate area of the polygon');
  // Calculates the area of a large polygon (approximating South Africa).
  double areaInSquareMeters = geo.polygonArea(polygonData);
  print('Area: ${areaInSquareMeters.toStringAsFixed(2)} square meters\n\n');

  // --- 2. Distance and Bearing ---
  print('2. Calculate distance and direction between two points');
  // Calculates the great-circle distance and initial bearing between two points.
  final distanceAndDirection = geo.distanceAndBearing(
    durbanCityHall, // from constants.dart
    capeTownCityHall, // from constants.dart
  );
  print(
    'Distance from Durban City Hall to Cape Town CityHall: ${distanceAndDirection["distance"]?.toStringAsFixed(2)} meters\n',
  );
  print(
    'Bearing from Durban City Hall to CapeTownCityHall: ${distanceAndDirection["bearing"]?.toStringAsFixed(2)} degrees\n\n',
  );

  // --- 3. Polygon Perimeter ---
  print('3. Calculate perimeter of the polygon');
  // Calculates the sum of the lengths of all edges of the polygon.
  double perimeterInMeters = geo.polygonPerimeter(polygonData);
  print('Perimeter: ${perimeterInMeters.toStringAsFixed(2)} meters\n\n');

  // --- 4. Point in Circle ---
  print('4. Check if a point is inside a circle');
  // Checks if a test point is within a given radius of a center point.
  bool inCircle = geo.pointInCircle(
    durbanCityHall, // from constants.dart
    testPoint2Json, // from constants.dart
    circleRadius, // radius in meters
  );
  print('Point in circle: $inCircle\n\n');

  // --- 5. Point in Polygon ---
  print('5. Check if a point is inside a polygon');
  // Uses the Ray Casting algorithm to determine if a point is within a polygon's boundaries.
  bool inPolygon = geo.pointInPolygon(polygonData, durbanCityHall);
  print('Point in polygon: $inPolygon\n\n');

  // --- 6. Convex Hull ---
  print('6. Calculate convex hull');
  // Computes the smallest convex polygon that encloses all points in the dataset.
  final convexHull = geo.convexHull(polygonData);
  print('Convex hull: $convexHull\n\n');

  // --- 7. Expanded Convex Hull ---
  print('7. Calculate expanded convex hull');
  // Creates a new convex hull that is expanded outwards from the original hull by a set distance.
  final expandedHull = geo.expandedConvexHull(polygonData, 50.0);
  print('Expanded convex hull: $expandedHull\n\n');

  // --- 8. Vector Intersection ---
  print('8. Calculate intersection point of two vectors');
  // `vectorIntersection` finds the two points where the great circles of the vectors intersect.
  // One point is the intersection, the other is its antipode on the opposite side of the Earth.
  final List<Map<String, double>> potentialIntersectionPoints = geo
      .vectorIntersection(jsonVector1, jsonVector2);
  print('Potential intersection point 1: ${potentialIntersectionPoints[0]}');
  print('Potential intersection point 2: ${potentialIntersectionPoints[1]}\n');

  // The goal is to find which of the two potential intersection points is the
  // "correct" one for the line *segments*. A good heuristic is to find which
  // of the two points is closest to any of the original four endpoints of the vectors.
  // The true intersection of the segments should be relatively close to the segments themselves.

  print(
    'The most likely intersection point (closest to a vector endpoint) is: ${distanceToCenterPointInVector(potentialIntersectionPoints)}\n\n',
  );

  // --- 9. Vector Center Point ---
  print('9. Calculate center point of a vector');
  // Calculates the geographical midpoint of a line segment.
  final centerPoint = geo.vectorCenterPoint(twoPointsData);
  print('Center point: $centerPoint\n\n');

  // --- 10. Polygon Centroid ---
  print('10. Calculate centroid of a polygon');
  // Calculates the centroid of the polygon's vertices.
  final polygonCentroid = geo.polygonCentroid(polygonData);
  print('Polygon centroid: $polygonCentroid\n\n');
}
0
likes
140
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

A comprehensive Dart package for geospatial and geometric calculations, suitable for Flutter and server-side applications.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

vector_math

More

Packages that depend on geo_utilities