Google map as background to my sharpmap map not aligning correctly - google-maps

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;

Related

Oxyplot showing HeatMapSeries behind ScatterSeries

I am trying to combine LineSeries, ScatterSeries and HeatmapSeries on a single OxyPlot instance. I am able to show the first two just fine on the same plot and it looks like the following:
The axes for this are generated by the following code:
var xAxis = new DateTimeAxis();
xAxis.Key = "X";
xAxis.Position = AxisPosition.Bottom;
xAxis.AbsoluteMinimum = DateTimeAxis.ToDouble(CurrentPass.AOS);
xAxis.AbsoluteMaximum = DateTimeAxis.ToDouble(CurrentPass.LOS);
xAxis.AxislineColor = xAxis.TextColor = xAxis.TicklineColor = xAxis.MajorGridlineColor = OxyColors.DarkGray;
var yAxis = new LinearAxis();
yAxis.Key = "Y";
yAxis.Position = AxisPosition.Left;
yAxis.AbsoluteMinimum = 0.0;
yAxis.AbsoluteMaximum = MaximumFrequency;
yAxis.Maximum = MaximumFrequency;
yAxis.AxislineColor = yAxis.TextColor = yAxis.TicklineColor = yAxis.MajorGridlineColor = OxyColors.DarkGray;
If I add in a third axes for the Heatmap (not even a HeatMapSeries yet), I get the following:
The extra code here for the axis for the HeatMap is:
var heatmapAxis = new LinearColorAxis();
heatmapAxis.AbsoluteMinimum = 0.0;
heatmapAxis.AbsoluteMaximum = MaximumFrequency;
heatmapAxis.Palette = OxyPalettes.Gray(1024);
heatmapAxis.Key = "HeatMap";
I am not sure what's going on here. The line series still shows. And all the ScatterPoints from the scatterseries I have are definitely there - the tracker shows up and I can interact with the points (hover, click etc.). But the points don't show. If I add a HeatMapSeries, the HeatMapSeries data does show up as expected, the LineSeries data still shows up but no ScatterSeries data.
Again, the HeatMap data and the Scatter Series data show up individually, but never together.
Has anyone encountered this before? Are there workarounds?
Thanks,
Aditya
Sorry this is a bit late but just stumbled upon this. I had something similar with a line not showing up on a Heatmap and after some experimenting i realized the order in which the series' are added to the plot model make a big difference. Make sure you add your ScatterSeries after you've added your Heatmap to the plot model.

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});

Geotools - get beside parces

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);
}
}

how to use MySQL DB for EGmap extension in Yii

for example i have a MySQL database that contains latitude and longitude, how do I call these coordinates to be displayed in the map ? anyone can help me please?
Since your question and information are a bit unclear, these are my assumptions:
I assume that you have followed the instruction correctly to install the EGMap
extension into your yii application.
You want to place a marker at the said coordinates (latitude &
longitude).
Sample codes: (please adjust to fit your database tables and columns at the lines
commented as "// Get LatLong from table Location"). Place this straight into your view file example: protected/views/site/index.php
<!-- other html codes here -->
<div id="map-container">
<?php
// Get LatLong from table Location
$location=Location::model()->findByPk(1);
$latitude = $location->latitude;
$longitude = $location->longitude;
Yii::import('ext.gmap.*');
$gMap = new EGMap();
$gMap->setJsName('map');
$gMap->zoom = 10;
$mapTypeControlOptions = array(
'sensor'=>true,
'position'=> EGMapControlPosition::LEFT_BOTTOM,
'style'=>EGMap::MAPTYPECONTROL_STYLE_DROPDOWN_MENU
);
// Map settings
$gMap->mapTypeControlOptions= $mapTypeControlOptions;
$gMap->setWidth(800);
$gMap->setHeight(600);
$gMap->setCenter($latitude, $longitude);
// Prepare icon
$icon = new EGMapMarkerImage("http://google-maps-icons.googlecode.com/files/gazstation.png");
$icon->setSize(32, 37);
$icon->setAnchor(16, 16.5);
$icon->setOrigin(0, 0);
// Prepare marker
$marker = new EGMapMarker($latitude, $longitude, array('title' => 'Gas Station','icon'=>$icon));
$gMap->addMarker($marker);
$gMap->renderMap();
?>
</div>
<!-- other html codes here -->

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!