A React component that displays GeoServer layers using WMS (Web Map Service) with Leaflet.
- React-based component architecture
- WMS layer support from GeoServer
- Style selection and management
- Automatic zoom to layer extent
- Interactive configuration panel
- Password protection
- Error handling and validation
- Easy to customize and integrate
- Install dependencies:
npm install-
Set up password protection (optional but recommended):
- Copy
.env.exampleto.env:cp .env.example .env
- Edit
.envand set your password:VITE_SITE_PASSWORD=your_secure_password_here - For GitHub Pages deployment, add the password as a GitHub Secret (see Deployment section)
- Copy
-
Start the development server:
npm run dev- Open your browser to the URL shown in the terminal (typically
http://localhost:5173)
- The app will open with default configuration
- Use the configuration panel to set your GeoServer settings:
- GeoServer URL: Your GeoServer instance URL (e.g.,
http://localhost:8080/geoserver) - Workspace: The workspace name (e.g.,
topp,cite, etc.) - Layer Name: The name of the layer to display
- CRS: Coordinate Reference System (default: EPSG:3857 for Web Mercator)
- Style Name: Optional style name from GeoServer (leave empty for default)
- Custom Legend Title: Optional custom title for the legend (leave empty to use layer title from GeoServer)
- GeoServer URL: Your GeoServer instance URL (e.g.,
- Click "Fetch Styles" to see available styles for the layer
- Click "Load Layer" to display the GeoServer layer
- The map will automatically zoom to the layer's extent
- The legend will appear in the bottom-right corner with the layer title
- GeoServer URL:
http://localhost:8080/geoserver - Workspace:
topp - Layer Name:
states
Edit src/components/GeoServerMap.jsx and modify the initial map view:
mapInstanceRef.current = L.map(mapRef.current).setView([39.8283, -98.5795], 4)The coordinates [39.8283, -98.5795] represent the center of the United States, and 4 is the zoom level.
Edit src/App.jsx to change the default configuration:
const [config, setConfig] = useState({
geoserverUrl: 'http://localhost:8080/geoserver',
workspace: 'topp',
layerName: 'states'
})geoserver_test/
├── src/
│ ├── components/
│ │ ├── GeoServerMap.jsx # Main map component
│ │ └── GeoServerMap.css # Component styles
│ ├── App.jsx # Main app component
│ ├── App.css # App styles
│ ├── main.jsx # React entry point
│ └── index.css # Global styles
├── index.html # HTML template
├── package.json # Dependencies
└── vite.config.js # Vite configuration
- React: 18.2.0 - UI library
- Leaflet: 1.9.4 - Open-source JavaScript library for mobile-friendly interactive maps
- ESRI Leaflet: 3.0.10 - Plugin for Leaflet to work with ArcGIS services
- Vite: 5.0.8 - Build tool and dev server
npm run buildThe built files will be in the dist directory.
This project is configured for automatic deployment to GitHub Pages using GitHub Actions.
-
Configure Base Path:
- Update the
basepath invite.config.jsto match your repository name - For this repo (
CarbonDemo), it's already set to/CarbonDemo/ - If your repository IS named
username.github.io, set base to'/':
base: '/' - Update the
-
Set Password for Production (if using password protection):
- Go to your repository → Settings → Secrets and variables → Actions
- Click New repository secret
- Name:
VITE_SITE_PASSWORD(must be exactly this, case-sensitive) - Value: Your desired password
- Click Add secret
- The workflow is already configured to use this secret (see
.github/workflows/deploy.yml) - For detailed instructions, see GITHUB_SECRET_SETUP.md
-
Push to Main Branch:
- The GitHub Actions workflow will automatically build and deploy when you push to
mainormasterbranch - You can also manually trigger deployment from the Actions tab → Deploy to GitHub Pages → Run workflow
- The GitHub Actions workflow will automatically build and deploy when you push to
-
Enable GitHub Pages (after first deployment):
- Go to your repository on GitHub
- Navigate to Settings → Pages
- Under "Source", select Deploy from a branch
- Select the
gh-pagesbranch and/ (root)folder - Click Save
- Alternatively, if you see "GitHub Actions" as an option, you can select that instead
-
Access Your Site:
- After deployment, your site will be available at:
https://devgateway.github.io/CarbonDemo/(for this repository)https://username.github.io/repo-name/(for other repos)
- You'll be prompted for the password when accessing the site
- After deployment, your site will be available at:
If you prefer to deploy manually:
-
Install
gh-pagespackage:npm install --save-dev gh-pages
-
Add deploy script to
package.json:"scripts": { "deploy": "npm run build && gh-pages -d dist" }
-
Update
vite.config.jsbase path as described above -
Deploy:
npm run deploy
- 404 Errors: Make sure the
basepath invite.config.jsmatches your repository name - Assets Not Loading: Check that all asset paths are relative (Vite handles this automatically)
- CORS Issues: Ensure your GeoServer allows cross-origin requests from your GitHub Pages domain
GeoServer provides powerful styling capabilities through SLD (Styled Layer Descriptor) and CSS. Here's what you can do:
- Access GeoServer Admin: Navigate to
http://your-geoserver:8080/geoserver/web - Go to Styles: Click "Styles" in the left menu
- Create New Style: Click "Add a new style"
- Choose Style Format:
- SLD (Styled Layer Descriptor): XML-based, most powerful
- CSS: Simpler syntax, good for basic styling
- YSLD: YAML-based, easier to read than SLD
Color Ramp Examples:
- Heat map: Red to yellow for high values, blue for low values
- Grayscale: Black to white gradient
- Rainbow: Full spectrum colors
- Custom: Define your own color stops
In SLD, you can use:
<ColorMap>
<ColorMapEntry color="#0000FF" quantity="0" label="Low"/>
<ColorMapEntry color="#00FFFF" quantity="50" label="Medium"/>
<ColorMapEntry color="#FFFF00" quantity="100" label="High"/>
<ColorMapEntry color="#FF0000" quantity="200" label="Very High"/>
</ColorMap>- Fill colors: Solid colors, patterns, or gradients
- Stroke colors and widths: Border styling
- Point symbols: Markers, icons, custom shapes
- Labels: Text styling and placement
- Opacity: Transparency settings
- Go to Layers: Click "Layers" in GeoServer admin
- Select Your Layer: Find and click on your layer
- Edit Layer: Click "Edit" tab
- Set Default Style: Choose a style from the dropdown
- Publish: Click "Save" to apply
- Default Style: Leave "Style Name" field empty to use the layer's default style
- Named Style: Enter the exact style name from GeoServer
- Fetch Styles: Click "Fetch Styles" button to see all available styles for your layer
- Select from Dropdown: After fetching, choose a style from the dropdown
For Better Visibility:
- Use high contrast color ramps (e.g., dark blue to bright yellow)
- Adjust opacity to see underlying basemap
- Use colorbrewer.org for color scheme ideas
- Consider your data range when setting color stops
For Carbon/Environmental Data:
- Green to red: Low to high carbon
- Blue to red: Cool to warm
- Grayscale: Simple, professional look
- Custom: Match your organization's color scheme
For dynamic styling, you can also use the SLD_BODY parameter in WMS requests, but this requires sending the full SLD XML. The current app uses the simpler STYLES parameter with pre-defined style names.
The app automatically displays a legend for your layer:
- Automatic Title: The legend uses the layer title from GeoServer's GetCapabilities
- Custom Title: You can override the title by entering a custom legend title in the configuration panel
- Style-Aware: The legend updates automatically when you change styles
- Toggleable: Click the × button to hide, or the 🗺️ button to show the legend again
- Responsive: Long titles are truncated with ellipsis, full title shown on hover
For the best legend titles:
-
Set Layer Title in GeoServer:
- Go to GeoServer Admin → Layers → Select your layer
- Click "Edit" tab
- Set a descriptive "Title" (e.g., "Carbon Prediction 2024 - 100m Resolution")
- Click "Save"
-
Use Custom Legend Title in App:
- Enter a custom title in the "Custom Legend Title" field
- This overrides the GeoServer layer title
- Useful for shorter, more concise titles
-
Best Practices:
- Keep titles concise but descriptive
- Include units if relevant (e.g., "Carbon (tons/ha)")
- Include date/version if applicable
- Use title case for readability
- Make sure your GeoServer instance is accessible from your browser
- If you encounter CORS issues, you may need to configure GeoServer to allow cross-origin requests
- The component uses WMS (Web Map Service) to fetch tiles from GeoServer
- The component handles layer cleanup and re-initialization properly
- Styles must be published in GeoServer before they can be used
- The map automatically zooms to the layer's extent when loaded
- The legend title is automatically fetched from GeoServer's GetCapabilities response