简体   繁体   中英

Converting Latitude and Longitude(double) data to byte array in order to store in mySql as Geometry or GeometryCollection value in java

My requirement is to store latitude and longitude coordinates( Double dataType values) fetched from a mobile webservice on java server and store in mySQL db as Geometry or Geometry collection datatype. I've created the table using one of the below queries:

CREATE TABLE `mydb`.`location`(    
`coords` GEOMETRYCOLLECTION);

or

CREATE TABLE `mydb`.`location`(
`coords` GEOMETRY);

And in NetBeans inside my project, I've created an entity to store values from webservice to the db. And when I've created the entity for the table in my db, the coords column from db is reverse engineered as byte[] in the entity(since the geometry/geometrycollection is a blob in db).

From the client device(lets say a mobile), I get the latitude and longitude as Double values from webService to my java server something like:

lati:  21.0826801  lng:  80.2707184

Now I need to convert those coordinate values which are Double to byte[ ] , such that it should look like a spatial data that is derived from one of the 3 queries below:

Query Type 1:

INSERT INTO location VALUES (geomfromwkb(point(9.197915773, 45.476819539)));

Query Type 2:

INSERT INTO location VALUES (geometrycollection(point(9.197915773, 45.476819539)));

Query Type 3:

INSERT INTO location VALUES (geomcollfromwkb(point(9.197915773, 45.476819539)));

How could I achieve it? Sorry for the long post, I've searched all around the web including stack overflow and found nothing relevant to my current scenario.

The first and third give you a "point", as seen via

mysql> select ASTEXT(geomfromwkb(point(9.197915773, 45.476819539)));
+-------------------------------------------------------+
| ASTEXT(geomfromwkb(point(9.197915773, 45.476819539))) |
+-------------------------------------------------------+
| POINT(9.197915773 45.476819539)                       |
+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select ASTEXT(geometrycollection(point(9.197915773, 45.476819539)));
+--------------------------------------------------------------+
| ASTEXT(geometrycollection(point(9.197915773, 45.476819539))) |
+--------------------------------------------------------------+
| GEOMETRYCOLLECTION(POINT(9.197915773 45.476819539))          |
+--------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select ASTEXT(geomcollfromwkb(point(9.197915773, 45.476819539)));
+-----------------------------------------------------------+
| ASTEXT(geomcollfromwkb(point(9.197915773, 45.476819539))) |
+-----------------------------------------------------------+
| POINT(9.197915773 45.476819539)                           |
+-----------------------------------------------------------+
1 row in set (0.01 sec)

I see no need for byte[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM