This article is part of a series of tutorials that show you how to develop your first application:
Goal
In this tutorial, we create a basic HTML page containing a LuciadRIA map. We use Vite, which serves our source files directly using native ES modules during development, and can bundle them into a single optimized artifact for production. Vite also comes with its own built-in dev server, which is useful for local development.
|
Although Vite features prominently, the goal of this tutorial is to show you how to integrate LuciadRIA into a web application. It doesn’t focus on the setup and use of Vite as such, nor on the project configuration for TypeScript. The use of Vite isn’t required: LuciadRIA comes as a set of ES6 modules, so you can use the tool chain of your choosing to add LuciadRIA to your web application. LuciadRIA doesn’t impose any special requirements. |
Project structure setup
As a first step, we create a new project directory with an index.html file and use Vite to serve this file.
These steps are based on the Vite Getting Started guide.
We start by creating a directory, initializing npm, configuring the project as an ES module and installing Vite and TypeScript:
mkdir hello-world
cd hello-world
npm init -y
npm pkg set type="module"
npm install vite typescript --save-dev
Vite serves the project root directory by default, so we set up the TypeScript sources in a src directory and put index.html in the project root.
mkdir src
# Already create some empty files, which we'll populate later
# The windows alternative for touch is type
# type nul > index.html
touch index.html #create an empty index.html file in the project root
touch src/index.ts #create an empty index.ts file in the src directory
For TypeScript, we need a tsconfig.json file specifying the compiler options.
For now, we just tell the TypeScript compiler tsc to create one for us:
npx tsc --init --target es2015 #this will create a tsconfig.json file
Depending on your project, you can change the settings in that file. For now, the default values are enough.
Vite has built-in support for TypeScript: it transpiles .ts files on the fly during development, so no loader configuration is needed.
No configuration file is needed for this tutorial: Vite works out of the box for this kind of setup.
While the dev server is running, it automatically picks up changes we make to our TypeScript code, and re-transpiles them to JavaScript.
index.html file
<!doctype html>
<html lang="en">
<head>
<title>Hello world</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
The resulting folder and file structure should look like
hello-world
|- index.html
|- node_modules
|- package.json
|- package-lock.json
| - src
|- index.ts
|- tsconfig.json
To actually start the development server, we add a start:dev script in package.json:
package.json file
"scripts": {
"start:dev": "vite"
}
Now we can run
npm run start:dev
and browse to http://localhost:5173/ where we’re greeted by our web page.
Installing the LuciadRIA npm packages
Your LuciadRIA distribution comes with a number of NPM packages under the packages folder.
-
The
@luciad/riapackage has the core modules of the LuciadRIA library -
The
@luciad/geometryand@luciad/symbologypackages are optional packages. Depending on the LuciadRIA tier you bought, you may not have access to them. Consult the LuciadRIA product tiers documentation for more information.
In this tutorial, we assume that these packages are already published in an npm registry. We’re going to install them using npm install.
# Install the package from your own private registry
npm install --registry http://my-private-registry:8073/ @luciad/ria
# Optionally: if you have purchased additional components, install them as well
# We won't be using any code from those packages in the remainder of this tutorial
# npm install --registry http://my-private-registry:8073/ @luciad/ria-geometry
# npm install --registry http://my-private-registry:8073/ @luciad/ria-milsym
We can now use the installed @luciad/ria package in our index.ts file.
|
An alternative to using the
Consult the npm documentation for more information. |
Adding a map to our application
First, we add a <div> to our index.html page to mark where we want our map.
Because we’re updating the HTML file, we immediately trigger the loading of our JavaScript code.
<body> element, containing a <div> for the map and the <script> tag for the JS in the index.html file
<body>
<h1>Hello world</h1>
<!-- Create a div where our map should appear -->
<div id="map"/>
<!-- Load the TypeScript file as a native ES module. Vite transpiles and serves src/index.ts directly during development. -->
<script type="module" src="/src/index.ts"></script>
</body>
We also add some CSS to give this <div> a certain size.
To keep things simple in this tutorial, we add the CSS directly to the header of our file:
index.html file
<style>
#map {
position: relative;
width: 100%;
height: 620px;
overflow: hidden;
border: 1px solid grey;
}
</style>
The complete index.html file
<!doctype html>
<html lang="en">
<head>
<title>Hello world</title>
<style>
#map {
position: relative;
width: 100%;
height: 620px;
overflow: hidden;
border: 1px solid grey;
}
</style>
</head>
<body>
<h1>Hello world</h1>
<div id="map"/>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
Now it’s time to write our first lines of LuciadRIA code.
In the src/index.ts file we need to create a new @luciad/ria/view/RIAMap instance, and tell it to appear in that <div>:
import {RIAMap} from "@luciad/ria/view/RIAMap.js";
//Create a new map instance, and display it in the div with the "map" id
const map = new RIAMap("map");
That’s it. We created our first LuciadRIA map.
If you visit the web page at this point, you bump into two problems, though:
-
We didn’t install the LuciadRIA license in our application yet, resulting in an error because the license can’t be found.
-
The map doesn’t contain any data, so the page doesn’t show anything.
Installing and activating the license
To activate the license, we’ll need to pass the contents of the license file to setLicenseText before creating the map:
-
Copy the
luciadria_development.txtlicense file to thesrcfolder. -
Add the following license setup code at the top of
src/index.ts, before creating the map:import {setLicenseText} from "@luciad/ria/util/License.js"; import licenseText from "./luciadria_development.txt?raw"; setLicenseText(licenseText);We’re using Vite’s built-in
?rawimport suffix to load the license file as a string.Make sure to set the license before creating the
RIAMapinstance. -
Vite ships its own TypeScript type definitions for Vite-specific features, such as the
?rawimport suffix we just used. Addvite/clientto thetypesarray in thecompilerOptionsof thetsconfig.jsonfile so that TypeScript recognizes this import:"compilerOptions": { "types": ["vite/client"] }
Adding some data to the map
Now that we installed the license, it’s time to add some data to the map so that we can see the map. For this tutorial, we connect to a public WMS server and display a dataset from that server.
We request the 92c09725-a9c5-46fb-bffd-d9e23b4abbf2 dataset from the https://sampleservices.luciad.com/wms server.
Once we have the connection to the server, we store the data in a layer and add it to our map.
import {WMSTileSetModel} from "@luciad/ria/model/tileset/WMSTileSetModel.js";
import {RasterTileSetLayer} from "@luciad/ria/view/tileset/RasterTileSetLayer.js";
//Add some WMS data to the map
const server = "https://sampleservices.luciad.com/wms";
const dataSetName = "4ceea49c-3e7c-4e2d-973d-c608fb2fb07e";
WMSTileSetModel.createFromURL(server, [{layer: dataSetName}])
.then(model => {
//Once the data is available, create a layer for it
const layer = new RasterTileSetLayer(model);
//and add the layer to the map
map.layerTree.addChild(layer);
});
|
Consult the WMS documentation for more information on how to deal with WMS data in LuciadRIA. |
This results in
Full code
src/index.ts file
import {setLicenseText} from "@luciad/ria/util/License.js";
import licenseText from "./luciadria_development.txt?raw";
import {RIAMap} from "@luciad/ria/view/RIAMap.js";
import {WMSTileSetModel} from "@luciad/ria/model/tileset/WMSTileSetModel.js";
import {RasterTileSetLayer} from "@luciad/ria/view/tileset/RasterTileSetLayer.js";
setLicenseText(licenseText);
//Create a new map instance, and display it in the div with the "map" id
const map = new RIAMap("map");
//Add some WMS data to the map
const server = "https://sampleservices.luciad.com/wms";
const dataSetName = "4ceea49c-3e7c-4e2d-973d-c608fb2fb07e";
WMSTileSetModel.createFromURL(server, [{layer: dataSetName}])
.then(model => {
//Once the data is available, create a layer for it
const layer = new RasterTileSetLayer(model);
//and add the layer to the map
map.layerTree.addChild(layer);
});
index.html file
<!doctype html>
<html lang="en">
<head>
<title>Hello world</title>
<style>
#map {
position: relative;
width: 100%;
height: 620px;
overflow: hidden;
border: 1px solid grey;
}
</style>
</head>
<body>
<h1>Hello world</h1>
<div id="map"/>
<script type="module" src="/src/index.ts"></script>
</body>
</html>