Skip to content

Instantly share code, notes, and snippets.

@Rachael-E
Last active August 6, 2024 10:08
Show Gist options
  • Select an option

  • Save Rachael-E/98a16974a500ed02ffa747e36fe3c0ea to your computer and use it in GitHub Desktop.

Select an option

Save Rachael-E/98a16974a500ed02ffa747e36fe3c0ea to your computer and use it in GitHub Desktop.
// load the data
void loadCoffeeCountries() async {
const jsonFilePath = 'assets/pathto.geojson';
try {
final contents = await services.rootBundle.loadString(jsonFilePath);
final coffeeCountries = coffeeCountriesDataFromJson(contents);
coffeeCountries.features.sort(
(a, b) => a.properties.admin.compareTo(b.properties.admin));
coffeeCountries.features
.map((feature) => _coffeeFeatures.add(feature))
.toList();
// ...
}
// get single part and multipart polygon geometry
Geometry? _getFeatureGeometry(CoffeeFeature feature) {
final coffeeFeatureCoordinatesList = feature.geometry.coordinates;
Geometry? polygon;
if (feature.geometry.type == CountryGeometryType.polygon) {
// if country is a single part polygon
final polygonBuilder =
PolygonBuilder.fromSpatialReference(SpatialReference.wgs84);
for (final coordinates in coffeeFeatureCoordinatesList[0]) {
final lat = coordinates[0];
final long = coordinates[1];
polygonBuilder.addPoint(
ArcGISPoint(
x: lat,
y: long,
spatialReference: SpatialReference.wgs84,
),
);
}
polygon = polygonBuilder.toGeometry();
} else if (feature.geometry.type == CountryGeometryType.multiPolygon) {
// if country is a multipart polygon
final polygonBuilderFromParts =
PolygonBuilder.fromSpatialReference(SpatialReference.wgs84);
for (final part in coffeeFeatureCoordinatesList) {
final mutablePart =
MutablePart.withSpatialReference(SpatialReference.wgs84);
for (final list in part) {
for (final coordinates in list) {
final lat = coordinates[0];
final long = coordinates[1];
mutablePart.addPoint(
ArcGISPoint(
x: lat,
y: long,
spatialReference: SpatialReference.wgs84,
),
);
}
}
polygonBuilderFromParts.parts.addPart(mutablePart: mutablePart);
}
polygon = polygonBuilderFromParts.toGeometry();
}
return polygon;
}
// create, display and add attributes to graphic from geometry
void _createAndDisplayGraphic(Geometry geometry, CoffeeFeature coffeeFeature) {
final simpleFillSymbol = SimpleFillSymbol(
style: SimpleFillSymbolStyle.solid,
color: _colorManager.getUniqueColor(),
outline: _borderSymbol,
);
final graphic = Graphic(geometry: geometry, symbol: simpleFillSymbol);
graphic.attributes.addAll({
'coffeeBagsProduced': coffeeFeature.properties.coffeeProduction,
'countryName': coffeeFeature.properties.admin,
});
_graphicsOverlay.graphics.add(graphic);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment