Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ignore_for_file: avoid_print
import 'package:h3_core/h3_core.dart';

void main() {
// Convert a coordinate to an H3 cell index at resolution 9.
final sf = LatLng(37.7749295, -122.4194155);
final cell = latLngToCell(sf, 9);
print('H3 cell: ${cell.toHex()}'); // e.g. 8928308280fffff

// Get the center of the cell.
final center = cellToLatLng(cell);
print('Center: ${center.lat}, ${center.lng}');

// Inspect the cell.
print('Resolution: ${getResolution(cell)}');
print('Valid: ${isValidCell(cell)}');
print('Pentagon: ${isPentagon(cell)}');

// Get the cell boundary vertices.
final boundary = cellToBoundary(cell);
print('Boundary vertices: ${boundary.vertices.length}');

// Find neighbors within 1 grid step.
final neighbors = gridDisk(cell, 1);
print('Neighbors (k=1): ${neighbors.length}');

// Grid distance between two cells.
final other = neighbors.last;
final distance = gridDistance(cell, other);
print('Grid distance: $distance');

// Hierarchy: parent and children.
final parent = cellToParent(cell, 8);
print('Parent (res 8): ${parent.toHex()}');

final children = cellToChildren(cell, 10);
print('Children (res 10): ${children.length}');

// Compact and uncompact.
final compacted = compactCells(children);
print('Compacted: ${compacted.length}');

// Measurements.
final area = cellAreaKm2(cell);
print('Cell area: ${area.toStringAsFixed(4)} km²');

final dist = greatCircleDistanceKm(sf, LatLng(40.7128, -74.0060));
print('SF to NYC: ${dist.toStringAsFixed(1)} km');

// Directed edges.
final neighbor = neighbors[1];
if (areNeighborCells(cell, neighbor)) {
final edge = cellsToDirectedEdge(cell, neighbor);
print('Edge: ${edge.toHex()}');
print('Edge length: ${edgeLengthKm(edge).toStringAsFixed(3)} km');
}

// Region: fill a polygon with cells.
final polygon = GeoPolygon([
LatLng(37.78, -122.42),
LatLng(37.78, -122.41),
LatLng(37.77, -122.41),
LatLng(37.77, -122.42),
]);
final filled = polygonToCells(polygon, 9);
print('Cells in polygon: ${filled.length}');

// Version info.
print('h3_core ${H3Version.package}, H3 native ${H3Version.native}');
}
10 changes: 9 additions & 1 deletion lib/src/errors.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/// Base exception for all H3 errors.
/// Exception thrown when an H3 operation fails.
///
/// Each error has a numeric [code] corresponding to the H3 C library error
/// codes and a human-readable [message].
class H3Exception implements Exception {
/// The H3 error code (1–19).
final int code;

/// A human-readable description of the error.
final String message;

/// Creates an [H3Exception] with the given [code] and [message].
const H3Exception(this.code, this.message);

/// Creates an [H3Exception] from an H3 C library error [code].
factory H3Exception.fromCode(int code) {
return switch (code) {
1 => const H3Exception(1, 'Operation failed'),
Expand Down
Loading