Method for quickly writing OSM data into and out of Postgresql database
By using XmlReader and XmlWriter objects in OSM data processing to process OSM data in batches, it solves the problems of low efficiency in OSM data import and export and difficulty in data sharing in the existing technology, realizes efficient data interaction and storage, supports multi-environment operation, and improves Improved data scalability and update speed.
Patent Information
- Application Number
- CN202410812020.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-21
- Publication Date
- 2024-09-17
AI Technical Summary
The existing technology is inefficient in the import and export process of OSM data, cannot realize data sharing and backup, has poor data scalability, is not updated in a timely manner, and cannot effectively save and feedback updated data.
Provides a method to quickly write and write OSM data by creating a PostgreSQL database connection, using XmlReader and XmlWriter objects to insert and query data in batches, generate a complete basic table structure, and write and read data to files to support data batch processing and file sharing.
It realizes the efficient interaction between OSM data and PostgreSQL database. The generated data is stored completely and has a high reuse rate. It supports operations such as road network generation and transportation facility maintenance. The execution is not restricted by the environment and is convenient for running in local or Internet environments.
Smart Images

Figure CN118656372A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer map data processing, and in particular to a method for quickly writing OSM data into and out of a Postgresql database. Background Art
[0002] In the field of geographic information systems and map services, OpenStreetMap (OSM) data is increasingly used. These data are usually stored in a PostgreSQL database and use the PostGIS extension to add support for geospatial data. Users can directly download data within a selected range from the website. OSM data elements mainly include three types: Nodes, Ways, and Relations. These three elements constitute the entire map. Among them, Nodes define the location of points in space; Ways define lines or areas; Relations (optional) define the relationship between elements.
[0003] PostgreSQL (PG) is a free, open-source database management system that follows the PostgreSQL protocol and provides support for SQL and full support for transactions. PostGIS is a spatial database extension that adds geographic object support to PostgreSQL. It standardizes geospatial data in sp mode (i.e., simplefeatures) that can be carried by relational databases, making the storage and query of geospatial data simpler and more intuitive. With PostGIS, you can easily create, edit, query, and analyze geospatial data, providing powerful support for various applications.
[0004] Importing and exporting OSM files to PostgreSQL database is a common GIS data processing task. Currently, the popular import and export methods are completed using related conversion tools, such as: osm2pgsql tool, ogr2ogr tool, and osmosis tool; these tools are used to execute their own command line commands to complete the interaction between data. This method requires preprocessing files, downloading conversion tools and different command line parameter configurations. Different tools also need to use tool-related command line statements to complete data writing and writing, which is very inconvenient to operate. And it can only be completed locally, and data sharing, data backup and recovery cannot be achieved, so the conversion efficiency is low.
[0005] Chinese invention patent CN107704531B discloses a method for automatically merging intersection nodes in OSM data. The method generates a node database and a road section database using OSM data files, extracts intersection nodes that need to be merged from the two databases, and merges multiple nodes of intersections that meet certain requirements to improve the speed of road network analysis and calculation, and avoids errors that may be caused by the failure to prohibit the intersection entrance and exit road connection. The OSM data processed by this method is partial file data, and the association relationship between the three elements of OSM data, namely, the nodes, ways and relations, is not generated, that is, the OSM file content is directly parsed, and the library table structure is not formed. The data scalability is poor, the update is not timely, and the application scope is limited.
[0006] Chinese invention patent CN117648783A discloses a method for constructing a regional integrated transportation network based on OSM. The method constructs an integrated regional integrated transportation network that can include highway sections and nodes, railway lines and nodes, water transport lines and nodes and related attributes, and quickly provides basic data in computer expression for regional integrated transportation analysis. This method uses multiple direct traversals of OSM files to complete the screening of data sets. The actual transportation network needs to be continuously updated and iterated. Therefore, this method cannot save the updated data, and the updated data cannot be fed back in time. The derived data of this method cannot be effectively retained for a long time and shared with other applications; the basic data of node, way, and relation are processed into other traffic object data such as rid, road, droad, cross, etc., which has poor scalability and low efficiency. Summary of the invention
[0007] The technical problem to be solved by the present invention is how to quickly write and write OSM file data into a Postgresql database.
[0008] In order to solve the above technical problems, the present invention provides a method for quickly writing OSM data into and out of a Postgresql database, the method comprising:
[0009] S10. Prepare data:
[0010] S11, obtaining the OSM data file of the target area;
[0011] S12, create a Postgresql database connection, create a new database osmosis and create a basic table for writing the OSM data;
[0012] S20. Write OSM data into Postgresql database:
[0013] S21, creating a new file map.osm, replacing the target field in the OSM data with the replacement string and writing the string into the new file map.osm;
[0014] S22, creating an XmlReader object to read the OSM data, and setting a sink method for the XmlReader object;
[0015] S23, creating a new data set of the write basic table to save the batch data of the write basic table to add OSM entity class object data, and batch inserting the OSM entity class object data into the write basic table;
[0016] S24, executing the read.run method of the XmlReader object to write the OSM data into the Postgresql database;
[0017] S30. Write Postgresql database data into OSM data file:
[0018] S31, creating a new file mapdata.osm, creating an XmlWriter object to write the Postgresql database data into the new file mapdata.osm, creating a write-out basic table for the OSM data, and querying the data set of the write-out basic table from the Postgresql database;
[0019] S32, retrieving the OSM entity class object data from the write-out basic table, and writing the OSM entity class object data into the OSM data file;
[0020] S33. Execute the complete method of the XmlWriter object to write the Postgresql database data into the OSM data file.
[0021] Furthermore, the method also includes uploading the OSM data to Minio buckets for file sharing.
[0022] Furthermore, the elements of the OSM data include Nodes, Ways and Relations; the write basic table includes nodes, node_tags, ways, way_tags, way_nodes, relations, relation_tags and relation_members.
[0023] Furthermore, the S21 also includes downloading the OSM data file from the Minio, Scanner reading the OSM data, BufferedWriter writing the OSM data into the new file map.osm, and replacing the target field in the OSM data with a replacement string.
[0024] Furthermore, the target fields are nodes, ways and relations.
[0025] Furthermore, the data set newly written into the basic table in S23 specifically includes:
[0026] By getting the entity class entity in sink.process();
[0027] Determine whether entity contains Node. If so, create a Node entity and assign a value to it, add the Node entity to the nodes collection, extract tags information from the Node entity, and store the tags information in the node_tags collection;
[0028] Determine whether entity contains Way. If so, create a Way entity and assign a value, add the Way entity to the ways collection, extract tags information from the Way entity, and store the tags information in the way_tags collection; extract way_nodes information from the Way entity, and store the way_nodes information in the way_nodes collection;
[0029] Determine whether entity contains Relation. If so, create a Relation entity and assign values, add the Relation entity to the relations collection, extract tags information from the Relation entity, and store the tags information in the relation_tags collection; extract members information from the Relation entity, and store the members information in the relation_members collection.
[0030] Furthermore, S23 also includes creating an INSERT statement corresponding to the write base table, which is used to cyclically insert the OSM entity class object data into the Postgresql database cache, and defines that the amount of data inserted in each batch does not exceed 1000.
[0031] Furthermore, the S31 also includes creating a SELECT statement corresponding to the write-out basic table, which is used to query the data set of the write-out basic table in the Postgresql database.
[0032] Furthermore, the S32 also includes:
[0033] Create a nodeTags collection to store Tags object information, and create a nodeTagsMap collection to store all nodeTags information of a node;
[0034] Create a wayTags collection to store the Tags object information, and create a wayTagsMap collection to store all wayTags information of the way;
[0035] Create a relationTags collection to store the Tags object information, and create a relationTagsMap collection to store all the relationTags information of the relation;
[0036] Create a relationMembers collection to store RelationMember object information, and create a relationMembersMap collection to store all relationMembers information of relation;
[0037] Create a wayNodes collection to store wayNode object information, and create a wayNodesMap collection to store all wayNodes information of the way;
[0038] Create a nodes collection to store all information of Node objects;
[0039] Create a ways collection to store all information of Way objects;
[0040] Create a relations collection to store all information of Relation objects.
[0041] Furthermore, the S33 includes executing the process method of the XmlWriter object to write the data of the nodes collection, the ways collection and the relations collection into the OSM data file.
[0042] Compared with the prior art, the present invention has the following beneficial effects:
[0043] 1. The OSM data file of the present invention can interact with the database directly without the need for tools and command lines.
[0044] 2. The basic data generated by the present invention is stored completely and has a high reuse rate, and other derivative data can be quickly generated. These data provide very important data support for a series of operations such as road network generation, road network node topology judgment, road traffic analysis, and traffic facility maintenance.
[0045] 3. The execution of the method in the present invention is not restricted by the environment and can be run locally, in an Internet environment, or in a virtual machine environment, which is very convenient. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 The present invention is a flowchart disclosed in an embodiment of the present invention. DETAILED DESCRIPTION
[0047] In order to make the technical scheme and technical effect of the present invention clearer, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments.
[0048] The present invention provides a method for quickly writing OSM data into and out of a Postgresql database. Figure 1 , the specific methods include:
[0049] 1. Prepare data
[0050] 1. Download the OSM map data file of the target area.
[0051] 2. Create a Postgresql database connection, create a new database osmosis and create the basic table for writing OSM data. Use Navicat Premium or DBeaver to create a Postgresql database connection, then create a new database osmosis and create a Postgresql database extension. There are three types of OSM data elements: nodes, ways and relations; there are 8 basic tables for writing, namely nodes, node_tags, ways, way_tags, way_nodes, relations, relation_tags, and relation_members.
[0052] 2. Writing OSM data into Postgresql database
[0053] 1. Replace the OSM data content and write it to the Postgresql database.
[0054] Create a new file map.osm, replace the target field in the OSM data with the replacement string and write it to the new file map.osm. Download the OSM data file from Minio, Scanner reads each line of the OSM data file, BufferedWriter writes each line of the content to the new file map.osm, and replaces the node / way / relation target field with the replacement string when it encounters it. After reading the OSM data file, close the reading and writing process.
[0055] 2. Create a Sink object to process OSM entities.
[0056] The Sink object mainly includes initialize, process, and complete methods.
[0057] 3. Create an XmlReader object to read OSM data and set the sink method for the XmlReader object.
[0058] 4. Create a new data set written into the basic table to save the batch data written into the basic table to add OSM entity class object data, and batch insert OSM entity class object data into the write basic table.
[0059] By getting the entity class entity in sink.process();
[0060] Determine whether entity contains Node. If so, create a Node entity and assign a value to it, add the Node entity to the nodes collection, extract tags information from the Node entity, and store the tags information in the node_tags collection;
[0061] Determine whether entity contains Way. If so, create a Way entity and assign a value to it. Add the Way entity to the ways collection. Extract tags information from the Way entity and store the tags information in the way_tags collection. Extract way_nodes information from the Way entity and store the way_nodes information in the way_nodes collection.
[0062] Determine whether entity contains Relation. If so, create a Relation entity and assign values, add the Relation entity to the relations collection, extract tags information from the Relation entity, and store the tags information in the relation_tags collection; extract members information from the Relation entity, and store the members information in the relation_members collection.
[0063] 5. Create the corresponding INSERT statement for writing into the basic table, which is used to insert the data in step 4 into the Postgresql database cache in a loop.
[0064] Create 8 basic INSERT statements for writing into the basic table, define the amount of data to be inserted in each batch as BATCH_SIZE = 1000, the size of the data set in step 4 cannot exceed BATCH_SIZE each time, and then insert the data into the Postgresql database cache in a loop. It should be noted that data resources must be released in a timely manner.
[0065] 6. Execute the read.run method of the XmlReader object, output the print prompt, and successfully write the OSM data into the Postgresql database.
[0066] Execute the run() method, which means that the file reading and data insertion start. After completion, there will be a print prompt. At this point, the OSM data is written into the Postgresql database.
[0067] 3. Writing Postgresql database data into OSM data files
[0068] 1. Create a new file mapdata.osm and create an XmlWriter object to write the Postgresql database data to the new file mapdata.osm.
[0069] Create the SELECT statement corresponding to the write-out basic table of OSM data, which is used to query the data set of the write-out basic table in the Postgresql database. The data sets of the eight write-out basic tables are: nodesResultSet, nodeTagsResultSet, waysResultSet, wayTagsResultSet, wayNodesResultSet, relationResultSet, relationTagsResultSet, relationMembersResultSet. Query the data set of the write-out basic table from the Postgresql database.
[0070] 2. Retrieve node tags information from the Nodes, Ways, and Relations tables, retrieve waynodes information from the way_nodes table, and retrieve members information from the relation_members table.
[0071] 1) Create a nodeTags collection to store the Tags object information, and create a nodeTagsMap collection to store all nodeTags information of the node.
[0072] Traverse the nodeTagsResultSet collection, idn = 0, nodeId = nodeTagsResultSet.getLong("node_id"). In the first iteration, initialize idn = nodeId; then determine whether nodeId and idn are equal:
[0073] If they are equal, a new Tags object is created, the k and v information in the nodeTagsResultSet is stored in the tags entity class, and then tags is added to the nodeTags collection;
[0074] If they are not equal, add idn and nodeTags to nodeTagsMap, clear nodeTags, idn = nodeId, create a new Tags object, store the k and v information in nodeTagsResultSet into the tags entity class, and then add tags to the nodeTags collection; until the last piece of data in nodeTagsResultSet, determine that the nodeTags collection is not empty, and add idn and nodeTags to nodeTagsMap.
[0075] 2) Create a wayTags collection to store the Tags object information, and create a wayTagsMap collection to store all wayTags information of the way.
[0076] Traverse the wayTagsResultSet collection, idw = 0, wayId = wayTagsResultSet.getLong ("way_id").
[0077] In the first iteration, initialize idw = wayId; then determine whether wayId is equal to idw:
[0078] If they are equal, a new Tags object is created, the k and v information in wayTagsResultSet is stored in the tags entity class, and then tags is added to the wayTags collection;
[0079] If they are not equal, add idw and wayTags to wayTagsMap, clear wayTags, idw = wayId, create a new Tags object, store the k and v information in wayTagsResultSet into the tags entity class, and then add tags to the wayTags collection; until the last piece of data in wayTagsResultSet, determine that the wayTags collection is not empty, and add idw and wayTags to wayTagsMap.
[0080] 3) Create a relationTags collection to store the Tags object information, and create a relationTagsMap collection to store all the relationTags information of the relation.
[0081] Traverse the relationTagsResultSet collection, idr = 0, relationId = relationTagsResultSet.getLong("relation_id"). In the first iteration, initialize idr = relationId; then determine whether relationId is equal to idr:
[0082] If they are equal, a new Tags object is created, the k and v information in the relationTagsResultSet is stored in the tags entity class, and then tags is added to the relationTags collection;
[0083] If they are not equal, add idr and relationTags to relationTagsMap, clear relationTags, set idr=relationId, create a new Tags object, store the k and v information in relationTagsResultSet into the tags entity class, and then add tags to the relationTags collection; until the last piece of data in relationTagsResultSet, determine that the relationTags collection is not empty, and add idr and relationTags to relationTagsMap.
[0084] 4) Create a relationMembers collection to store the RelationMember object information, and create a relationMembersMap collection to store all the relationMembers information of the relation.
[0085] Traverse the relationMembersResultSet collection, idrm = 0, relationId = relationMembersResultSet.getLong("relation_id"). In the first iteration, initialize idrm = relationId; then determine whether relationId is equal to idrm:
[0086] If they are equal, a new RelationMember object is created, the memberId, memberType, and memberRole information in relationMembersResultSet is stored in the RelationMember entity class, and then the RelationMember is added to the relationMembers collection;
[0087] If they are not equal, add idrm,relationMembers to relationMembersMap, clear relationMembers, idrm = relationId, create a new RelationMember object, store the memberId, memberType, memberRole information in relationMembersResultSet into the RelationMember entity class, and then add RelationMember to the relationMembers collection; until the last piece of data in relationMembersResultSet, determine that the relationMembers collection is not empty, and add idrm,relationMembers to relationTagsMap.
[0088] 5) Create a wayNodes collection to store wayNode object information, and create a wayNodesMap collection to store all wayNodes information of the way.
[0089] Traverse the wayNodesResultSet collection, idwn = 0, wayId = wayNodesResultSet.getLong("way_id"). In the first iteration, initialize idwn = wayId; then determine whether wayId is equal to idwn:
[0090] If they are equal, a new wayNode object is created, the lat and lon information in wayNodesResultSet is stored in the wayNode entity class, and then the wayNode is added to the wayNodes collection;
[0091] If they are not equal, add idwn and wayNodes to wayNodesMap, clear wayNodes, set idwn = wayId, create a new wayNode object, store the lat and lon information in wayNodesResultSet into the wayNode entity class, and then add wayNode to the wayNodes collection; until the last piece of data in wayNodesResultSet, determine that the wayNodes collection is not empty, and add idwn and wayNodes to wayNodesMap.
[0092] 6) Create a nodes collection to store all information of the Node object.
[0093] Traverse nodesResultSet, obtain the nodeId, version, tstamp, changesetId, lat, lon field value information in nodesResultSet and assign it to the Node object, then obtain the tags value from the nodeTagsMap in step 1) and assign it to the Node object, and add the Node object to the nodes collection.
[0094] 7) Create a ways collection to store all information of Way objects.
[0095] Traverse waysResultSet, obtain the wayId, version, tstamp, changesetId field value information in waysResultSet and assign it to the Way object, then obtain the tags value from wayTagsMap in step 2) and assign it to the Way object, and add the Way object to the ways collection.
[0096] 8) Create a relations collection to store all information of Relation objects.
[0097] Traverse relationsResultSet, obtain the relationId, version, tstamp, changesetId field value information in relationsResultSet and assign them to the Relation object, then obtain the tags value from the relationTagsMap in step 3) and assign it to the Relation object; obtain the relationMembers value from the relationMembersMap in step 4) and assign it to the Relation object, and add the Relation object to the relations collection.
[0098] 3. Execute the complete method of the XmlWriter object to write the nodes, ways, and relations collection data in step 6), step 7), and step 8) in step 2 above into the OSM data file.
[0099] 1) Traverse the nodes collection, create an EntityContainerFactory object, then create an EntityContainer for the Node entity class, write the Node entity class EntityContainer object to the XML writer, namely process(entityContainer) of XmlWriter, and write the data into the OSM data file.
[0100] 2) Traverse the ways collection, create an EntityContainerFactory object, then create an EntityContainer for the Way entity class, write the Way entity class EntityContainer object to the XML writer, namely process(entityContainer) of XmlWriter, and write the data into the OSM data file.
[0101] 3) Traverse the relation collection, create an EntityContainerFactory object, then create an EntityContainer for the Relation entity class, write the Relation entity class EntityContainer object into the XML writer, namely process(entityContainer) of XmlWriter, and write the data into the OSM data file.
[0102] 4. Execute the complete method of the XmlWriter object to output a print prompt, and the data in the Postgresql database is successfully written into the OSM data file.
[0103] After the complete method is executed, a prompt will be printed. At this point, the Postgresql database data is written into the OSM data file.
[0104] Upload OSM data to Minio buckets for file sharing. Files in the cloud can be downloaded at any time without being lost.
[0105] The Postgresql database data imported by the present invention can be directly applied to the city's road traffic as basic traffic analysis data; the basic data can be derived to meet the requirements of more complex road traffic; the exported OSM file data is uploaded to the cloud, open for sharing and download, and can be used for secondary application development, greatly saving manpower and time costs.
[0106] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for quickly writing OSM data into a Postgresql database, characterized in that: The method comprises: S10. Prepare data: S11, obtaining the OSM data file of the target area; S12, create a Postgresql database connection, create a new database osmosis and create a basic table for writing the OSM data; S20. Write OSM data into Postgresql database: S21, creating a new file map.osm, replacing the target field in the OSM data with the replacement string and writing the string into the new file map.osm; S22, creating an XmlReader object to read the OSM data, and setting a sink method for the XmlReader object; S23, creating a new data set of the write basic table to save the batch data of the write basic table to add OSM entity class object data, and inserting the OSM entity class object data into the write basic table in batches; S24, executing the read.run method of the XmlReader object to write the OSM data into the Postgresql database; S30. Write Postgresql database data into OSM data file: S31, creating a new file mapdata.osm, creating an XmlWriter object to write the Postgresql database data into the new file mapdata.osm, creating a write-out basic table for the OSM data, and querying the data set of the write-out basic table from the Postgresql database; S32, retrieving the OSM entity class object data from the write-out basic table, and writing the OSM entity class object data into the OSM data file; S33. Execute the complete method of the XmlWriter object to write the Postgresql database data into the OSM data file.
2. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The method also includes uploading the OSM data to Minio buckets for file sharing.
3. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The elements of the OSM data include Nodes, Ways and Relations; the write basic table includes nodes, node_tags, ways, way_tags, way_nodes, relations, relation_tags and relation_members.
4. The method for quickly writing OSM data into and out of a Postgresql database according to claim 2, characterized in that: The S21 also includes downloading the OSM data file from the Minio, Scanner reading the OSM data, BufferedWriter writing the OSM data into the new file map.osm, and replacing the target field in the OSM data with a replacement string.
5. The method for quickly writing OSM data into and out of a Postgresql database according to claim 4, characterized in that: The target fields are nodes, ways, and relations.
6. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The data set newly written into the basic table in S23 specifically includes: By getting the entity class entity in sink.process(); Determine whether entity contains Node. If so, create a Node entity and assign a value to it, add the Node entity to the nodes collection, extract tags information from the Node entity, and store the tags information in the node_tags collection; Determine whether entity contains Way. If so, create a Way entity and assign a value, add the Way entity to the ways collection, extract tags information from the Way entity, and store the tags information in the way_tags collection; extract way_nodes information from the Way entity, and store the way_nodes information in the way_nodes collection; Determine whether entity contains Relation. If so, create a Relation entity and assign values, add the Relation entity to the relations collection, extract tags information from the Relation entity, and store the tags information in the relation_tags collection; extract members information from the Relation entity, and store the members information in the relation_members collection.
7. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The S23 also includes creating an INSERT statement corresponding to the write basic table, which is used to cyclically insert the OSM entity class object data into the Postgresql database cache, and defines that the amount of data inserted in each batch does not exceed 1000.
8. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The S31 also includes creating a SELECT statement corresponding to the write-out basic table, which is used to query the data set of the write-out basic table in the Postgresql database.
9. The method for quickly writing OSM data into and out of a Postgresql database according to claim 1, characterized in that: The S32 also includes: Create a nodeTags collection to store Tags object information, and create a nodeTagsMap collection to store all nodeTags information of a node; Create a wayTags collection to store the Tags object information, and create a wayTagsMap collection to store all wayTags information of the way; Create a relationTags collection to store the Tags object information, and create a relationTagsMap collection to store all the relationTags information of the relation; Create a relationMembers collection to store RelationMember object information, and create a relationMembersMap collection to store all relationMembers information of relation; Create a wayNodes collection to store wayNode object information, and create a wayNodesMap collection to store all wayNodes information of the way; Create a nodes collection to store all information of Node objects; Create a ways collection to store all information of Way objects; Create a relations collection to store all information of Relation objects.
10. The method for quickly writing OSM data into and out of a Postgresql database according to claim 9, characterized in that: The S33 includes executing the process method of the XmlWriter object to write the data of the nodes collection, the ways collection and the relations collection into the OSM data file.
Citation Information
Patent Citations
Method and apparatus for importing contents of XML file into database
CN105320763A
Method for automatically generating urban road network through OSM map data
CN116821267A
Regional comprehensive traffic network fusion construction method based on OSM
CN117648783A
XML document processing
US20020123993A1
