Geotools - get beside parces - geotools

I have a problem needed help. I have parces map layer in postgis which contain polygons. This layer is following by not overlap topology rule.
How could i get features that "beside" a selected feature with geotools api? The "beside" feature is feature have at least one same edge.
For example in this picture, when select feature A, i need to get feature B,C,D, not get feature E.
Any help is high appreciate! Thanks!

I solve my problem by this code
public static void getRoundFeature(SimpleFeature feature, SimpleFeatureSource featureSource){
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
Geometry g = (Geometry) feature.getAttribute("the_geom");
Polygon polygon = geometryFactory.createPolygon(g.getCoordinates());
//Polygon poly = (Polygon) feature;
Filter filter = ff.intersects(ff.property("the_geom"), ff.literal(polygon));
try {
SimpleFeatureCollection featureCollection = featureSource.getFeatures(filter);
//System.out.println("feature around: "+ featureCollection.size());
FeatureIterator iter = featureCollection.features();
while(iter.hasNext()){
SimpleFeature rfeature = (SimpleFeature) iter.next();
System.out.println("so to: "+rfeature.getAttribute("soto")+ "so thua: "+rfeature.getAttribute("sothua"));
}
} catch (IOException e) { LOGGER.log(Level.FINER, e.getMessage(), e);
}
}

Related

Google map as background to my sharpmap map not aligning correctly

I've been at this for quite a while now.
What I'm trying to achieve here is to add Google map as background layer to my Sharpmap, I'm able to achieve this but the problem I'm facing now is My map always centre's to a point near a Greenland sea in Google Map , it's like it won't take my centre point coordinates.
I'm using Sharpmap 1.1 with BruTile 0.7.4.4
So far I've done the below.
SharpMap.Map _map = new SharpMap.Map();
SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("parcel");
SharpMap.Data.Providers.MsSqlSpatial DBlayer = new SharpMap.Data.Providers.MsSqlSpatial(_connectionString, "XXXXXX", "XXXX", "XXX");
layer.Style.Fill = new SolidBrush(Color.Transparent);
layer.Style.Outline = new Pen(Color.Black);
layer.Style.EnableOutline = true;
layer.MaxVisible = 13000;
layer.DataSource = DBlayer;
ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
layer.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
layer.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
//SharpMap.Layers.TileLayer layerBackground = new TileLayer(new BingTileSource(BingRequest.UrlBing, "", BingMapType.Aerial), "TileLayer");
SharpMap.Layers.TileLayer layerBackground = new TileLayer(new GoogleTileSource(GoogleMapType.GoogleMap), "googlemaps");
_map.Layers.Add(layerBackground);
_map.Layers.Add(layer);
_map.BackColor = Color.White;
//-98.526890,29.411539
_map.Center = new GeoAPI.Geometries.Coordinate(0,0);
return _map;
Even if I manually give Geo Coordinates It just points to the same spot in the sea.
Please see the below Google map Geo point, this is the point where my map shows as centre. Every time I generate no matter what I do it shows this point as its centre.
71.946088, -3.956171
Any help is much appreciated. Thanks and Cheers!
I found the problem for Google map layer not centring.
The reason is that I used QGIS to convert my ESRI shapefile from WGS84(EPSG:4326) format to Spherical Mercator(EPSG:900913) and it changed the coordinates format but
Google Maps use Google Maps Global Mercator (Spherical Mercator).
When I used an online converter to test this out which you can find here. When I gave the resulting coordinates, it worked out just fine. Now all I have to do is find a way to convert Google Maps Global Mercator (Spherical Mercator).
Thanks for your help #pauldendulk.
I was running into the same problem and in theirExamples they provide a LatLongToGoogle transformation function
public static ICoordinateTransformation LatLonToGoogle()
{
CoordinateSystemFactory csFac = new CoordinateSystemFactory();
CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
IGeographicCoordinateSystem sourceCs = csFac.CreateGeographicCoordinateSystem(
"WGS 84",
AngularUnit.Degrees,
HorizontalDatum.WGS84,
PrimeMeridian.Greenwich,
new AxisInfo("north", AxisOrientationEnum.North),
new AxisInfo("east", AxisOrientationEnum.East));
List<ProjectionParameter> parameters = new List<ProjectionParameter>
{
new ProjectionParameter("semi_major", 6378137.0),
new ProjectionParameter("semi_minor", 6378137.0),
new ProjectionParameter("latitude_of_origin", 0.0),
new ProjectionParameter("central_meridian", 0.0),
new ProjectionParameter("scale_factor", 1.0),
new ProjectionParameter("false_easting", 0.0),
new ProjectionParameter("false_northing", 0.0)
};
IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
IProjectedCoordinateSystem targetCs = csFac.CreateProjectedCoordinateSystem(
"Google Mercator",
sourceCs,
projection,
LinearUnit.Metre,
new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));
ICoordinateTransformation transformation = ctFac.CreateFromCoordinateSystems(sourceCs, targetCs);
return transformation;

Why does Google Maps containsLocation never find a point inside my polygon?

I'm having issues with the following code in that my points never find a point within a polygon using google.maps.geometry.poly.containsLocation. You can see the full code and that there are indeed points in the polygon here http://htinteractive.com/crime_map_fairview.html However in the console you will see that none of the points were found to be in the polygon per the containsLocation function.
I feel like maybe I'm not passing in one the values correctly to the containsLocation function, but so far what I've found in the documentation this seems correct.
var myLatlng = new google.maps.LatLng(lat,lon);
fairview = [
new google.maps.LatLng(39.161536, -86.535107),
new google.maps.LatLng(39.17885, -86.534825),
new google.maps.LatLng(39.179068, -86.547164),
new google.maps.LatLng(39.180989, -86.551803),
new google.maps.LatLng(39.181546, -86.556001),
new google.maps.LatLng(39.170956, -86.569335),
new google.maps.LatLng(39.158487, -86.570365),
new google.maps.LatLng(39.161482, -86.566674),
new google.maps.LatLng(39.157156, -86.559807),
new google.maps.LatLng(39.160084, -86.553961),
new google.maps.LatLng(39.160317, -86.550700),
new google.maps.LatLng(39.160733, -86.548464),
new google.maps.LatLng(39.161482, -86.546172),
new google.maps.LatLng(39.161536, -86.535107)
];
var pv = new google.maps.Polygon(fairview);
if (google.maps.geometry.poly.containsLocation(myLatlng, pv)) {
console.log("Location Found in Polygon!!!!! " + myLatlng.lat() + " " + myLatlng.lng());
} else {
console.log(":( " + myLatlng.lat() + " " + myLatlng.lng());
}
As Dr. Molle pointed out I was instantiating my polygon incorrectly.
var pv = new google.maps.Polygon({path:fairview});

How to Use .as Exported File from PhysicsEditor

The question was here for a long time with bounty and no satisfying solution for me. I erased the first post and am posting instead a question that can be answered quickly with a yes or no so I can proceed with my doings.
If you could answer it really fast before it's deleted by "not a good question". Is using a custom shape from PhysicsEditor to Nape the same as doing it with Box2D? (ofc changing syntax)
If you could then give a look in that link then say it's the same process in Nape that'll be enought thanks.
I ask this because I found the Box2D tutorial easier to follow so far.
public var floor:Body;
floor = new Body(BodyType.STATIC);
var floorShape:PhysicsData = new PhysicsData();
floor.shapes.add(floorShape); // Error: Implicit coercion of a value of type PhysicsData to an unrelated type nape.shape:Shape.
floor.space = space;
Update:
According to a comment on this blog post, it sounds like recent versions of Nape have broken compatibility with the physics editor. Specifically, the graphic and graphicUpdate properties no longer exist on body objects. The solution suggested is to remove references to those properties.
I'm not in a position to be able to test this, but you could try updating the createBody method of your floor class as follows:
public static function createBody(name:String /*,graphic:DisplayObject=null*/):Body {
var xret:BodyPair = lookup(name);
//if(graphic==null) return xret.body.copy();
var ret:Body = xret.body.copy();
//graphic.x = graphic.y = 0;
//graphic.rotation = 0;
//var bounds:Rectangle = graphic.getBounds(graphic);
//var offset:Vec2 = Vec2.get(bounds.x-xret.anchor.x, bounds.y-xret.anchor.y);
//ret.graphic = graphic;
/*
ret.graphicUpdate = function(b:Body):void {
var gp:Vec2 = b.localToWorld(offset);
graphic.x = gp.x;
graphic.y = gp.y;
graphic.rotation = (b.rotation*180/Math.PI)%360;
}
*/
return ret;
}

How to convert longitude and latitude value to address in windows phone? [duplicate]

This question already has an answer here:
Windows Phone ReverseGeocoding to get Address from Lat and Long
(1 answer)
Closed 8 years ago.
I get the latitude and longitude value using
void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
latitude = e.Position.Location.Latitude;
longitude = e.Position.Location.Longitude;
MessageBox.Show("Latitude & Longitude:" + latitude + " " + longitude);
}
Result like:
Latitude & Longitude: 12.56 77.34
Now i want to these value to relevant address. Is it possible. Please give the brief explanation. Thanks in advance.
Firstly, Your question is not clear, I think your requirement is from this lat/long you need to fetch the corresponding address. For doing so you can use Bing services, Bing is providing several API's that support the same. In you case you can use the following API
http://dev.virtualearth.net/REST/v1/Locations/47.64324%2c-122.14197?includeEntityTypes=Address,Postcode1,AdminDivision1,AdminDivision2&key={Bing Map Key}&o=json
From this the 47.64, and -122.11 are my sample geocodes, For achieving complete functionality you need to create a Bing Map key (here is the link for your reference )
after creating the key use it on the {Bing Map Key} area. Without brackets. Then a simple http GET method will solve your problem.
OR
Simply you can use,
var query = new ReverseGeocodeQuery {GeoCoordinate = new GeoCoordinate(12.56, 77.34)};
query.QueryCompleted += (s, e) =>
{
if(e.Error != null)
return;
var address = e.Result[0].Information.Address.Street;
};
query.QueryAsync();
In Windows Phone 8 you can do it easily using the integrated ReverseGeocodeQuery class. For example:
string address;
ReverseGeocodeQuery query = new ReverseGeocodeQuery();
query.GeoCoordinate = new GeoCoordinate(12.56, 77.34);
query.QueryCompleted += (s, e) =>
{
if (e.Error != null)
return;
address = e.Result[0].Information.Address.Street;
};
query.QueryAsync();
As simple as that.
For Windows Phone 7, however, you would need to use Bing services.

Centering Google Maps depending on pins

I have a ASP.NET website which contains a Google Map. I am using the GoogleMapForASPNet framework made by some clever and helpful individual.
Anywho, I have a problem with centering the map after inserting two pins.
Basically, I am calculating the middle point of the pins and setting that as the Center Point of the map.
Below is my code:
GooglePoint newPoint = new GooglePoint();
double newLat = 0;
double newLong = 0;
if (googlePointA.Latitude > googlePointB.Latitude)
{
newLat = googlePointA.Latitude - googlePointB.Latitude;
newPoint.Latitude = googlePointA.Latitude - newLat;
}
else
{
newLat = googlePointB.Latitude - googlePointA.Latitude;
newPoint.Latitude = googlePointB.Latitude + newLat;
}
if (googlePointA.Longitude > googlePointB.Longitude)
{
newLong = googlePointA.Longitude - googlePointB.Longitude;
newPoint.Longitude = googlePointA.Longitude - newLong;
}
else
{
newLong = googlePointB.Longitude - googlePointA.Longitude;
newPoint.Longitude = googlePointB.Longitude + newLong;
}
GoogleMapForASPNet1.GoogleMapObject.CenterPoint = newPoint;
GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 8;
It works half and half, but not properly. As in, when I feed different Pins, it doesn't really center the map, but close enough. Or sometimes, the other pin will be off the map but only an inch, which means the map isn't centered at all.
The zoom is static because the pins will always be close by, so there's no need for me to make it dynamic.
Any help is extremely, extremely, extremely appreciated.
Thank you.
The Google Maps API has an object to do that: google.maps.LatLngBounds
Just create a LatLngBounds object, add your pin coordinates to it with the extend() method, then use the map panToBounds(yourBounds) method and you're all set!