A guide to avoiding pitfalls in data source settings when GeoServer publishes services

Digression

        As time goes on, the annual May Day has come. As the first May Day after the epidemic, I don't know how my friends will spend this Labor Day? I decided to go to another city to observe and experience different scenery, or to travel and give myself a vacation. Yesterday, I was maxed out by a post on 12306 about all train tickets sold out at Shanghai Hongqiao Station on April 28. This fully demonstrates everyone's enthusiasm for traveling on May Day.

        At this moment, there must be some friends who have to work on duty for various reasons. Here, we pay tribute to the hard-working laborers from all walks of life. Those who struggle are the most beautiful, you who struggle are the most beautiful scenery on May Day, and you who work hard are the most brilliant existence in May! Salute to each of our hard workers.

I. Introduction

        In the previous blog , the method of using LeafLet to superimpose the Geoserver wms layer to the existing base map briefly explained the release of Wms and other services by Geoserver, and there is no specific introduction to the service release of GeoServer. The students in the previous project team encountered some problems when using GeoServer for service publishing. For example, the failure to create a data source, and the WMS service with Chinese language is not displayed correctly in the preview, but prompts for downloading. This article will focus on how to use GeoServer to publish WMS services, and the problems encountered when creating data sources. The service layer encounters the problem of downloading Chinese layer prompts. If you also encounter these problems in the production process, I hope I can help you.

2. GeoServer

1. Introduction to GeoServer

        This article is not an article focusing on GeoServer, oh. There are many articles about the introduction of GeoServer. Here is just a brief introduction. The following is the official introduction to geoserver github on GitHub , describing the main functions of this project.

         To put it simply, the GeoServer project is a complete Java (J2EE) system, which implements the OpenGIS Alliance's network function server specification and network coverage server specification (complete OGC standard implementation), and integrates Web map servers, all standard OGC Services, such as WMS, WMTS, WCS, WFS, vector slices, etc., can be directly supported.

2. GeoServer main interface

        The GeoServer version used in this article is shown in the figure below, and it is deployed in the form of war package, jdk1.8, tomcat uses 8.5

         After starting Tomcat, you can access the GeoServer application and publish the service.

 3. GeoServer service release

1. Log in to GeoServer

        GeoServer needs to be logged in before it can be used. The default account is: admin/geoserver.

 2. Create a new workspace

        Click the workspace menu on the left, then the list button on the right, and click the Add button to manage the workspace. The workspace is mainly used to manage different data, such as road data, water system data, and POI data, and manage them by category.

         Generally speaking, the namespace URI of the workspace only needs to be filled with one that does not have the same name. It is best to use the address of the official website plus a string that can indicate the type of work item. Generally, it is not easy to make mistakes here, just operate normally.

3. The new data source is abnormal

Click the New Data Source button to add and manage data sources. Data sources include vector, raster, and other data. As shown below.

         Here we take the vector data source as an example, click Shapefile - ESRI(tm) Shapefiles (*.shp), and enter the following page:

         Here, please pay attention to the name of the data source, which contains a colon, please pay special attention . Here is the data that causes the problem. After clicking Save, the system will report the following error.

         At the same time, an error was also found in the running console of Geoserver. The details are shown in the figure below:

         The corresponding directory cannot be created in the background. Here, let's go to the background to look at the corresponding directory.

         We found that each data source will have a corresponding directory for data binding. Open the datastore.xml file under the hnxjxqarea directory, and you can find the following definitions.

<dataStore>
  <id>DataStoreInfoImpl--3834fa5a:187c815698e:-7ffc</id>
  <name>hnxjxqarea</name>
  <description>湖南湘江新区</description>
  <type>Shapefile</type>
  <enabled>true</enabled>
  <workspace>
    <id>WorkspaceInfoImpl--3834fa5a:187c815698e:-7fff</id>
  </workspace>
  <connectionParameters>
    <entry key="charset">ISO-8859-1</entry>
    <entry key="filetype">shapefile</entry>
    <entry key="create spatial index">true</entry>
    <entry key="memory mapped buffer">false</entry>
    <entry key="timezone">Asia/Shanghai</entry>
    <entry key="enable spatial index">true</entry>
    <entry key="namespace">http://www.yelangking.com/gisdev</entry>
    <entry key="cache and reuse memory maps">true</entry>
    <entry key="skipScan">true</entry>
    <entry key="url">file://F:\vector_data\湘江新区\湘江新区-融合.shp</entry>
    <entry key="fstype">shape</entry>
  </connectionParameters>
  <__default>false</__default>
  <dateCreated>2023-04-28 13:44:33.890 UTC</dateCreated>
</dataStore>

4. Abnormal location

        In the console, you can see that there is a problem with line 324 of the class org.geoserver.platform.resource.FileSystemResourceStore.java. Let's go to its source code to see what business logic is executed in line 324.

         Since there are many source codes, not all of them are listed. Copy the codes involved in the above figure and have a look. It has been written very clearly here that the upper-level directory was not created correctly and an exception was thrown. Here we verify whether the file format with a colon can create a directory.

public File file() {
		if (!file.exists()) {
			try {
				File parent = file.getParentFile();
				if (!parent.exists()) {
					boolean created = parent.mkdirs();
					if (!created) {
						throw new IllegalStateException("Unable to create " + parent.getAbsolutePath());
					}
				}
				if (parent.isDirectory()) {
					Lock lock = lock();
					boolean created;
					try {
						created = file.createNewFile();
					} finally {
						lock.release();
					}
					if (!created) {
						throw new FileNotFoundException("Unable to create " + file.getAbsolutePath());
					}
				} else {
					throw new FileNotFoundException(
							"Unable to create" + file.getName() + " - not a directory " + parent.getAbsolutePath());
				}
			} catch (IOException e) {
				throw new IllegalStateException("Cannot create " + path, e);
			}
		}
		if (file.isDirectory()) {
			throw new IllegalStateException("Directory (not a file) at " + path);
		} else {
			return file;
		}
	}

        In Windows, just create a new folder and put a colon, usually the system will report the following warning:

         From this, the root of the problem has been completely found, and it must be remembered that in GeoServer, the name of the directory must not have special characters . In the Java world, many conventions are greater than configuration, and there are many predetermined rules. Once you find the root of the problem, you can solve the problem. You only need to remove the colon and save it.

5. Wms Chinese problem

        Is it true that at this point, the release of the map will go smoothly, and everything will be fine. That's not necessarily the case, everything needs to be verified before the data is displayed normally.

         Did some students encounter problems? In particular, the name in the layer contains Chinese, and there is no data display interface, but a download prompt. It's really the dark before the dawn, it's depressing. If students who have experience in web development have probably guessed the problem, it is the reason why the layer contains Chinese. The modification method is also very simple. In tomcat, set the request encoding of the container. In the tomcat installation directory, find the following code in server.xml:

<Connector port="8083" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

        Add encoding settings at the end of the file, as follows:

<Connector port="8083" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>

        Remember to save and restart the tomcat service. So far, the release of the data service has been completed, and the data source creation problems and Chinese layer access problems encountered on the road have all been resolved. Let's take a look at the actual preview effect.

 6. Use Leaflet to load Wms

<!DOCTYPE html>
<html>
<head>
	<title>geoserver全国地铁展示</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
	<link rel="stylesheet" href="/2d/leaflet/leaflet.css" />
    <script src="/2d/leaflet/leaflet.js?v=1.0.0"></script>
</head>
<body>
<div id="mapid" style="width: 100%; height: 600px;"></div>
<script>
	var mymap = L.map('mapid').setView([29.052934, 104.0625], 5);

	//加载wms服务的图层
	var dtLineLayer = L.tileLayer.wms(
		'http://localhost:8083/geoserver/testwzh/wms', {
			layers: 'testwzh:2022年全国地铁线数据',
			format: 'image/png',
			transparent: true
		}
	);
    dtLineLayer.addTo(mymap);

</script>

</body>
</html>

 Summarize

        The above is the main content of this article. This article will focus on how to use GeoServer to publish WMS services, and the problems encountered when creating data sources. The service layer encounters the problem of downloading Chinese layer prompts. If you also encounter these problems in the production process, I hope I can help you. The writing is hasty, and the shortcomings are still hopeful. At the same time, friends are welcome to leave a message in the comment area for criticism and correction.

        At the end of the text, I once again wish all my friends a happy holiday, enjoy the scenery when going out, and feel comfortable at home, and pay tribute to the laborers.

Guess you like

Origin blog.csdn.net/yelangkingwuzuhu/article/details/130439167