It really depends on which level you want to work at.
I am not entirely clear on whether you mean that you already have loaded the
shapefile in Java (using GeoTool's ShapeFile functionality). If not, you can
take a look at something like
http://docs.geotools.org/stable/userguide/library/data/shape.html
For a lot of math/analysis work, it is worth remembering that you have the
powerful JTS library underlying GeoTools, and sometimes it's easier to simply
dip into that and use the "raw" math.
For a simple spatial indexing, you could look at JTS's STRtree or Quadtree to
find "rivers roughly in the area" and just use the Geometry.distance function
to brute-force find the best match. (Or, well, just brute-force everything.
Odds are good you have so few features that naive bruteforcing is as fast as
any clever algorithms).
This can be a good way to get a feeling for how the algorithms work, without
having to worry about taming GeoTools. Of course, once you need more
complicated things, you'll find that GeoTools offer a lot of functionality, but
it can be overwhelming at first.
As for the "convert coordinates", that's one example of GeoTools having a lot
of utility functions once you know what to look for.
I don't know which coordinate system your file uses, but here is an example of
converting geometry from the UTM Euref89 Zone 32 (the one called epsg:25832) to
lat/lon (epsg:4326):
com.vividsolutions.jts.geom.Geometry g1 = new
com.vividsolutions.jts.geom.GeometryFactory().createPoint(new
com.vividsolutions.jts.geom.Coordinate(707763, 6176558));
org.opengis.referencing.crs.CoordinateReferenceSystem from =
org.geotools.referencing.CRS.decode("epsg:25832");
org.opengis.referencing.crs.CoordinateReferenceSystem to =
org.geotools.referencing.CRS.decode("epsg:4326");
org.opengis.referencing.operation.MathTransform transform =
org.geotools.referencing.CRS.findMathTransform(from, to);
com.vividsolutions.jts.geom.Geometry g2 =
org.geotools.geometry.jts.JTS.transform(g1, transform);
To use that code, you need to have a jar containing the CRS (Coordinate
reference system) definitions in the classpath, for example, maven-style:
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>14.0</version>
</dependency>
(It's good form to do org.geotools.referencing.CRS.cleanupThreadLocals(); once
you're done with the CRS class, but that's a bit tangential).
I hope this gives you some pointers to what to look for. An alternate take at
the completely other end of the spectrum could be to simply drop your shapefile
into a GeoServer installation (http://geoserver.org/) and let it do all the
clever things, or some other balance between "on the metal" and "powerful
standard functionality". (JTS, GeoTools and GeoServer are closely related,
roughly on a scale from "raw math" to "standardized web API").
JWN
From: João Bandeira [mailto:[email protected]]
Sent: 29. oktober 2015 11:17
To: [email protected]
Subject: [Geotools-gt2-users] Doubts about geotools usage. University project.
Hi!
My name is João, I'm from Portugal, and I'm a Software and Computer Engineering
student at FEUP. I'm contacting you about GeoTools.
I'm contacting users through the email list as a suggestion given by Andrea,
one of the geotools developers.
I'm currently developing a project for college where I have to interpret a map
of the rivers in my country. I have access to a file with info of all the
rivers. It's a file with the extension .shp, and I was already able to open it
using the geotools tools. I was able to open the map in the swing interface of
geotools, and see all the rivers, and also see information of each river by
clicking it.
After this, I was able, with a few lines of code, to search info about a river
by its name: I opened the file, and made a query with a filter applied to one
of the fields (designation of the river), and it will find all the rivers which
contain a given string in its designation.
When I wrote this code, I noticed that for each river, there is some
geometric(?) information about them, a list of coordinates. As I have no
knowledge about this type of files and its structure, I really don't know how
to interpret those coordinates, and I really needed some way to do that.
The functionality that I need to implement is, given a pair of coordinates
(latitude and longitude), check if it belongs to any river in the file, and
return info about it. I know that it should be possible because using the swing
interface, and clicking the river, it returns info about it, so I think that
somehow the program receives coordinates about the place I clicked, and
searches the file for a river containing those coordinates. Am I right?
If so, and if possible, what I needed to know is how to do that... My plan was
to reverse engineer the code with eclema for eclipse, but I couldn't import all
the source code to my project, so I didn't succeed, that's why I'm contacting
you. Probably what I want to do is simple, and a few guidelines from someone
who is familiarized with the project will suffice. My main problem is how to
convert latitude and longitude to the coordinate system used in the file I
have, and also match a pair of coordinates to the info in the file (to check if
they belong to any river).
If it helps, here is the file I'm working with: http://1drv.ms/1SaccYG
Sorry for bothering you with this, but you're probably my last resource, as
I've been struggling with this for the last week, with no luck...
Thanks and best regards,
João Norim
------------------------------------------------------------------------------
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users