ac_geojson 1.0.4
ac_geojson: ^1.0.4 copied to clipboard
GeoJson classes using JsonSerializable
example/ac_geojson_example.dart
/*
* Copyright 2025 Martin Edlman - Anycode <ac@anycode.dev>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:convert';
import 'package:ac_geojson/ac_geojson.dart';
void main() {
// Example 1: Creating and serializing a Point
createPoint();
// Example 2: Creating and serializing a LineString
createLine();
// Example 3: Creating and serializing a Polygon
createPolygon();
// Example 4: Creating a Feature object
createFeature();
// Example 5: Creating a FeatureCollection
createFeatureCollection();
// Example 6: Parsing GeoJSON string
parseGeoJson();
}
void createPoint() {
print('\n=== CREATING A POINT ===');
// Creating point coordinates
final coordinates = PointCoordinates(latitude: 50.0755, longitude: 14.4378); // Prague
// Creating a Point object
final point = GeoJsonPoint(coordinates: coordinates);
// Converting to JSON
final pointJson = point.toJson();
final encodedPoint = jsonEncode(pointJson);
print('Point object: $point');
print('Point as JSON: $encodedPoint');
}
void createLine() {
print('\n=== CREATING A LINESTRING ===');
// Creating coordinates for a line - Charles Bridge in Prague
final lineCoordinates = LineCoordinates([
PointCoordinates(latitude: 50.0865, longitude: 14.4112), // Start of the bridge
PointCoordinates(latitude: 50.0861, longitude: 14.4125), // Middle point
PointCoordinates(latitude: 50.0857, longitude: 14.4137), // End of the bridge
]);
// Creating a Line object
final line = GeoJsonLine(coordinates: lineCoordinates);
// Converting to JSON
final lineJson = line.toJson();
final encodedLine = jsonEncode(lineJson);
print('Line object: $line');
print('Line as JSON: $encodedLine');
}
void createPolygon() {
print('\n=== CREATING A POLYGON ===');
// Creating coordinates for a polygon - approximate Old Town Square
final outerRing = LineCoordinates([
PointCoordinates(latitude: 50.0880, longitude: 14.4208),
PointCoordinates(latitude: 50.0879, longitude: 14.4224),
PointCoordinates(latitude: 50.0873, longitude: 14.4222),
PointCoordinates(latitude: 50.0874, longitude: 14.4207),
PointCoordinates(latitude: 50.0880, longitude: 14.4208), // Closing the polygon
]);
// Creating PolygonCoordinates (outer boundary + optional holes)
final polygonCoordinates = PolygonCoordinates([outerRing]);
// Creating a Polygon object
final polygon = GeoJsonPolygon(coordinates: polygonCoordinates);
// Converting to JSON
final polygonJson = polygon.toJson();
final encodedPolygon = jsonEncode(polygonJson);
print('Polygon object: $polygon');
print('Polygon as JSON: $encodedPolygon');
}
void createFeature() {
print('\n=== CREATING A FEATURE OBJECT ===');
// Creating point coordinates
final coordinates = PointCoordinates(latitude: 50.0811, longitude: 14.4280); // Old Town Hall
// Creating a Point object
final point = GeoJsonPoint(coordinates: coordinates);
// Creating properties for the point
final properties = <String, dynamic>{
'id': 'landmark-1',
'name': 'Old Town Hall',
'built': 1338,
'height': 69.5,
'touristAttraction': true,
};
// Creating a Feature object
final feature = GeoJsonFeature(
geometry: point,
properties: properties,
);
// Converting to JSON
final featureJson = feature.toJson();
final encodedFeature = jsonEncode(featureJson);
print('Feature object: $feature');
print('Feature as JSON: $encodedFeature');
}
void createFeatureCollection() {
print('\n=== CREATING A FEATURE COLLECTION ===');
// Creating Point Feature objects
final feature1 = GeoJsonFeature(
geometry: GeoJsonPoint(
coordinates: PointCoordinates(latitude: 50.0811, longitude: 14.4280),
),
properties: {'id': 'landmark-1', 'name': 'Old Town Hall', 'type': 'landmark'},
);
final feature2 = GeoJsonFeature(
geometry: GeoJsonPoint(
coordinates: PointCoordinates(latitude: 50.0755, longitude: 14.4378),
),
properties: {'id': 'square-1', 'name': 'Wenceslas Square', 'type': 'square'},
);
// Creating a LineString Feature object
final feature3 = GeoJsonFeature(
geometry: GeoJsonLine(
coordinates: LineCoordinates([
PointCoordinates(latitude: 50.0865, longitude: 14.4112),
PointCoordinates(latitude: 50.0857, longitude: 14.4137),
]),
),
properties: {'id': 'bridge-1', 'name': 'Charles Bridge', 'type': 'bridge'},
);
// Creating a FeatureCollection
final collection = GeoJsonFeatureCollection(features: [feature1, feature2, feature3]);
// Converting to JSON
final collectionJson = collection.toJson();
final encodedCollection = jsonEncode(collectionJson);
print('FeatureCollection object: $collection');
print('FeatureCollection as JSON: $encodedCollection');
}
void parseGeoJson() {
print('\n=== PARSING GEOJSON STRING ===');
// GeoJSON string representing a point
final String pointJson = '''
{
"type": "Point",
"coordinates": [14.4378, 50.0755]
}
''';
// GeoJSON string representing a Feature
final String featureJson = '''
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [14.4378, 50.0755]
},
"properties": {
"name": "Wenceslas Square",
"type": "square"
},
"id": "square-1"
}
''';
// Parsing Point
final pointMap = jsonDecode(pointJson) as Map<String, dynamic>;
final point = GeoJsonPoint.fromJson(pointMap);
print('Parsed Point: $point');
// Parsing Feature
final featureMap = jsonDecode(featureJson) as Map<String, dynamic>;
final feature = GeoJsonFeature.fromJson(featureMap);
print('Parsed Feature: $feature');
print('Feature properties: ${feature.properties}');
// Parsing GeoJSON (generic)
try {
final geoJson = GeoJson.fromJson(featureMap);
print('Parsed GeoJson: $geoJson');
} catch (e) {
print('Error parsing GeoJson: $e');
}
}