@foodmarketmaker/mapag 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -225,7 +225,10 @@ class Styles {
225
225
  return;
226
226
  }
227
227
  // Fetch and cache the style definition if not already cached
228
- await this.fetchAndCacheStyle(styleData);
228
+ const styleSpec = await this.fetchAndCacheStyle(styleData);
229
+ if (!styleSpec) {
230
+ return;
231
+ }
229
232
  this.saveCustomSourcesAndLayers();
230
233
  this.map.once("styledata", () => {
231
234
  setTimeout(() => {
@@ -235,13 +238,23 @@ class Styles {
235
238
  this.restoreCustomSourcesAndLayers();
236
239
  }, 100);
237
240
  });
238
- this.map.setStyle(styleData.url);
241
+ // this.map.setStyle(styleData.url);
242
+ this.map.setStyle(styleSpec);
239
243
  this.style.set(styleData);
240
244
  this.currentStyleCode = styleData.code;
241
245
  if (this.onStyleChange) {
242
246
  this.onStyleChange(styleData);
243
247
  }
244
248
  }
249
+ async FetchStyleDefinition(styleData) {
250
+ const response = await fetch(styleData.url);
251
+ if (!response.ok) {
252
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
253
+ }
254
+ const styleSpec = await response.json();
255
+ styleSpec.glyphs = 'https://demotiles.maplibre.org/font/{fontstack}/{range}.pbf';
256
+ return styleSpec;
257
+ }
245
258
  async fetchAndCacheStyle(styleData) {
246
259
  // Skip if already cached
247
260
  if (this.styleCache[styleData.code]) {
@@ -258,6 +271,10 @@ class Styles {
258
271
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
259
272
  }
260
273
  const styleSpec = await response.json();
274
+ // Add Glyphs
275
+ if (!styleSpec.glyphs) {
276
+ styleSpec.glyphs = 'https://demotiles.maplibre.org/font/{fontstack}/{range}.pbf';
277
+ }
261
278
  // Extract source and layer IDs
262
279
  const sourceIds = Object.keys(styleSpec.sources || {});
263
280
  const layerIds = (styleSpec.layers || []).map((layer) => layer.id);
@@ -268,6 +285,7 @@ class Styles {
268
285
  layers: layerIds
269
286
  };
270
287
  this.styleCache[styleData.code] = enhancedStyleData;
288
+ return styleSpec;
271
289
  }
272
290
  catch (error) {
273
291
  console.error('Failed to fetch style definition for:', styleData.code, ':', error);
@@ -14082,17 +14100,20 @@ class MapComponent {
14082
14100
  ngOnDestroy() {
14083
14101
  this.styles.destroy();
14084
14102
  }
14085
- initializeMap() {
14086
- this.zone.runOutsideAngular(() => {
14103
+ async initializeMap() {
14104
+ this.zone.runOutsideAngular(async () => {
14087
14105
  const container = this.mapContainer();
14088
- let style = 'https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json';
14089
- // style = 'https://demotiles.maplibre.org/style.json'
14106
+ // let style =
14107
+ // 'https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json';
14108
+ // // style = 'https://demotiles.maplibre.org/style.json'
14109
+ let style = await this.styles.FetchStyleDefinition(this.styles.style());
14090
14110
  if (container) {
14091
14111
  // @ts-ignore - Type conflict between different maplibre-gl versions
14092
14112
  const map = new maplibregl.Map({
14093
14113
  container: container.nativeElement,
14094
14114
  // style: this.basemap(),
14095
- style: this.styles.style().url, // Default style
14115
+ // style: this.styles.style().url, // Default style
14116
+ style: style, // Default style
14096
14117
  center: [-98, 38.88],
14097
14118
  minZoom: 1,
14098
14119
  zoom: 3,
@@ -14898,10 +14919,18 @@ class VectorTileServerMapper {
14898
14919
  // }
14899
14920
 
14900
14921
  class WatershedMapper {
14922
+ static WBDHU2 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/WBDHU2.pmtiles';
14923
+ static WBDHU4 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/WBDHU4.pmtiles';
14924
+ static WBDHU6 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/watershedhu6.pmtiles';
14925
+ static WBDHU8 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/WBDHU8.pmtiles';
14926
+ static WBDHU10 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/WBDHU10.pmtiles';
14927
+ static WBDHU12 = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/WBDHU12.pmtiles';
14901
14928
  FILL_LAYER_ID = 'watershed-layer';
14902
14929
  LINE_LAYER_ID = 'watershed-layer-line';
14930
+ LABEL_LAYER_ID = 'watershed-layer-label';
14903
14931
  SOURCE_ID = 'watershedSource';
14904
14932
  SOURCE_LAYER = 'watershedhu6';
14933
+ currentHU = 'HUC6';
14905
14934
  settings = signal(new WatershedSettings(), ...(ngDevMode ? [{ debugName: "settings" }] : []));
14906
14935
  current = null;
14907
14936
  currentFeatureID = undefined;
@@ -14926,6 +14955,11 @@ class WatershedMapper {
14926
14955
  return;
14927
14956
  }
14928
14957
  const map = this.map;
14958
+ if (this.currentHU !== settings.hydrologicUnit) {
14959
+ this.clear();
14960
+ this.currentHU = settings.hydrologicUnit;
14961
+ this.create();
14962
+ }
14929
14963
  if (settings.visible) {
14930
14964
  map.setLayoutProperty(this.FILL_LAYER_ID, 'visibility', 'visible');
14931
14965
  map.setLayoutProperty(this.LINE_LAYER_ID, 'visibility', 'visible');
@@ -14939,13 +14973,48 @@ class WatershedMapper {
14939
14973
  map.setPaintProperty(this.LINE_LAYER_ID, 'line-color', settings.borderColor);
14940
14974
  map.setPaintProperty(this.LINE_LAYER_ID, 'line-width', settings.borderWidth);
14941
14975
  map.setPaintProperty(this.LINE_LAYER_ID, 'line-opacity', settings.borderOpacity);
14976
+ const watershedLabelVisible = settings.visible && settings.labelsVisible;
14977
+ map.setLayoutProperty(this.LABEL_LAYER_ID, 'visibility', watershedLabelVisible ? 'visible' : 'none');
14978
+ // map.setPaintProperty(this.LABEL_LAYER_ID, 'text-size', settings.labelsSize);
14979
+ map.setPaintProperty(this.LABEL_LAYER_ID, 'text-color', settings.labelsColor);
14980
+ map.setPaintProperty(this.LABEL_LAYER_ID, 'text-halo-color', settings.labelsHaloColor);
14981
+ map.setPaintProperty(this.LABEL_LAYER_ID, 'text-halo-width', settings.labelsHaloWidth);
14942
14982
  }
14943
14983
  create() {
14944
14984
  if (!this.map) {
14945
14985
  return;
14946
14986
  }
14947
14987
  const map = this.map;
14948
- const PMTILES_URL = 'pmtiles://https://foodmarketmaker-upload-data.s3.us-west-2.amazonaws.com/tiles/watershedhu6.pmtiles';
14988
+ let PMTILES_URL = WatershedMapper.WBDHU6;
14989
+ switch (this.settings().hydrologicUnit) {
14990
+ case 'HUC2':
14991
+ PMTILES_URL = WatershedMapper.WBDHU2;
14992
+ this.SOURCE_LAYER = 'WBDHU2';
14993
+ break;
14994
+ case 'HUC4':
14995
+ PMTILES_URL = WatershedMapper.WBDHU4;
14996
+ this.SOURCE_LAYER = 'WBDHU4';
14997
+ break;
14998
+ case 'HUC6':
14999
+ PMTILES_URL = WatershedMapper.WBDHU6;
15000
+ this.SOURCE_LAYER = 'watershedhu6';
15001
+ break;
15002
+ case 'HUC8':
15003
+ PMTILES_URL = WatershedMapper.WBDHU8;
15004
+ this.SOURCE_LAYER = 'WBDHU8';
15005
+ break;
15006
+ case 'HUC10':
15007
+ PMTILES_URL = WatershedMapper.WBDHU10;
15008
+ this.SOURCE_LAYER = 'WBDHU10';
15009
+ break;
15010
+ case 'HUC12':
15011
+ PMTILES_URL = WatershedMapper.WBDHU12;
15012
+ this.SOURCE_LAYER = 'WBDHU12';
15013
+ break;
15014
+ default:
15015
+ PMTILES_URL = WatershedMapper.WBDHU6;
15016
+ this.SOURCE_LAYER = 'watershedhu6';
15017
+ }
14949
15018
  AddSource(map, this.SOURCE_ID, {
14950
15019
  type: 'vector',
14951
15020
  url: PMTILES_URL,
@@ -14963,10 +15032,51 @@ class WatershedMapper {
14963
15032
  "source-layer": this.SOURCE_LAYER,
14964
15033
  type: 'line',
14965
15034
  }, StandardLayersMapper.POLYGONS_BACKGROUND);
15035
+ AddLayer(map, {
15036
+ id: this.LABEL_LAYER_ID,
15037
+ source: this.SOURCE_ID,
15038
+ "source-layer": this.SOURCE_LAYER,
15039
+ type: 'symbol',
15040
+ layout: {
15041
+ 'text-field': ['get', 'name'],
15042
+ },
15043
+ paint: {
15044
+ 'text-color': this.settings().labelsColor,
15045
+ 'text-halo-color': this.settings().labelsHaloColor,
15046
+ 'text-halo-width': this.settings().labelsHaloWidth,
15047
+ // 'text-size': this.settings().labelsSize,
15048
+ },
15049
+ }, StandardLayersMapper.POLYGONS_BACKGROUND);
14966
15050
  this._update(this.settings());
14967
15051
  if (!addedFill) {
14968
15052
  return;
14969
15053
  }
15054
+ map.on('zoomend', () => {
15055
+ if (!this.settings().visible) {
15056
+ return;
15057
+ }
15058
+ const zoom = map.getZoom();
15059
+ if (this.settings().autoSelectLayer) {
15060
+ if (zoom >= 0 && zoom < 5 && this.currentHU !== 'HUC2') {
15061
+ this.update({ hydrologicUnit: 'HUC2' });
15062
+ }
15063
+ else if (zoom >= 5 && zoom < 7 && this.currentHU !== 'HUC4') {
15064
+ this.update({ hydrologicUnit: 'HUC4' });
15065
+ }
15066
+ else if (zoom >= 7 && zoom < 9 && this.currentHU !== 'HUC6') {
15067
+ this.update({ hydrologicUnit: 'HUC6' });
15068
+ }
15069
+ else if (zoom >= 9 && zoom < 11 && this.currentHU !== 'HUC8') {
15070
+ this.update({ hydrologicUnit: 'HUC8' });
15071
+ }
15072
+ else if (zoom >= 11 && zoom < 13 && this.currentHU !== 'HUC10') {
15073
+ this.update({ hydrologicUnit: 'HUC10' });
15074
+ }
15075
+ else if (zoom >= 13 && this.currentHU !== 'HUC12') {
15076
+ this.update({ hydrologicUnit: 'HUC12' });
15077
+ }
15078
+ }
15079
+ });
14970
15080
  map.on('mousemove', this.FILL_LAYER_ID, (e) => {
14971
15081
  this.over.set(e.features && e.features.length > 0 ? e.features[0] : null);
14972
15082
  });
@@ -15006,6 +15116,7 @@ class WatershedMapper {
15006
15116
  if (this.map) {
15007
15117
  this.map.removeLayer(this.FILL_LAYER_ID);
15008
15118
  this.map.removeLayer(this.LINE_LAYER_ID);
15119
+ this.map.removeLayer(this.LABEL_LAYER_ID);
15009
15120
  this.map.removeSource(this.SOURCE_ID);
15010
15121
  }
15011
15122
  }
@@ -15016,12 +15127,19 @@ class WatershedMapper {
15016
15127
  popup = null;
15017
15128
  }
15018
15129
  class WatershedSettings {
15130
+ hydrologicUnit = 'HUC6';
15019
15131
  visible = true;
15020
15132
  fillColor = '#0000ff';
15021
15133
  fillOpacity = 0.1;
15022
15134
  borderColor = '#01018bff';
15023
15135
  borderWidth = 1;
15024
15136
  borderOpacity = 1.0;
15137
+ labelsVisible = true;
15138
+ labelsSize = 12;
15139
+ labelsColor = '#000000';
15140
+ labelsHaloColor = '#ffffff';
15141
+ labelsHaloWidth = 1;
15142
+ autoSelectLayer = false;
15025
15143
  }
15026
15144
 
15027
15145
  class HttpBoundaryLoader {