Coordinates of City Centers from OpenStreetMap via OSMnx? - osmnx

Is it possible to query OpenStreetMap via the Python library OSMnx to retrieve the (lat,long) coordinates for a city? The documentation seems to suggest attribute data should be accessible but I've not yet seen a pathway to retrieving the coordinates of a city's center. Using the OSMnx geocoder for a city of interest led to a poor result.

You may want to try to work with centroids - It's clearly not the city center in a colloquial definition, but it gives you the the center of the city boundaries:
gdf = ox.geocode_to_gdf('London').centroid
>>> POINT (-0.10942 51.50050
London's city center would be in ... erm ... Woolwich, but you can try "City of London"
gdf = ox.geocode_to_gdf('city of London')
>>> POINT (-0.09244 51.51441)
which gives you a location between Bank and St Paul's in... erm... "Trump Street"
Not what you are looking for I guess for various reasons, but it is the "center".
https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.centroid.html

Related

Why do we use crs.PlateCarree() instead of crs.Geodetic() when using Matplotlib and Cartopy to plot a map based on lat and lon?

I've been learning how to use Cartopy and Matplotlib to plot map. But I have a question regarding the argument transform. According to the Cartopy document, transform specifies "what coordinate system your data are defined in". Suppose I am going to plot temperatures of an area, and the area has been split into several grid cells. Each grid cells has a corresponding coordinate defined in lat and lon (Geodetic Systems). Based on the Cartopy document, I need to use crs.PlateCarree() instead of crs.Geodetic(). I'm a bit confused about it. Because,I think the PlateCarree is a way of projection. In other words, coordinates defined in PlateCarree projections are projected data. But latitude and longitude should be unprojected data. Can anyone help me with it? Thanks!

USA and Russia Geometry extracted from Bigquery has a visual distortion

I am using this query to extract the geometries of all countries using BigQuery public dataset, see question here
how to extract all countries geometry from Openstreet map dataset in BigQuery
I use R to draw the results
I tried Kepler.GL and gave me the same results
Something is wrong with Russia and the USA
I know little about R visualization, but what is probably happening is you getting WKT text from BigQuery, and feeding it to R, which has different assumptions.
Tthe issue is your R package probably treats WKT differently than BigQuery. WKT semantics depends on the spatial reference system (SRS) used, which could be geographic (non-projected, using sphere or ellipsoid) or projected (flat map). BigQuery uses geographic system, so edge between points A and B is the shortest geodesic path. Most visualization systems use projected coordinates, and assume flat map. Edge between A and B is shortest straight line on the flat map.
While this does not matter too much in many cases, it still does affect precision when you have long edges. But when an edge crosses anti-meridian (180 degree meridian) you get big problem. An edge between (-169, 66) (eastern edge of Russia) and say (176, 70) (a nearby point on Chukchi sea) is relatively short on the sphere, it crosses anti-meridian, and spans 15 degrees longitude. But the same edge on flat map span 145 degrees longitude and crosses most of the map! These are the long near-horizontal lines you see.
What should you do?
If R has a packet that supports geographic SRS (it is sometime an option to use geodesic edges), you could try it.
Or you can also let BigQuery convert geography from geographic SRS to flat map, that R would understand, using ST_AsGeoJson function. GeoJson is defined on flat map, so BigQuery ST_AsGeoJson converts the semantics from geographic SRS to flat map SRS. You then visualize GeoJson string instead of WKT string in R.
ST_AsGeoJson does a lot of work, to make result conformant to GeoJson spec and flat map. It splits parts of geography that lay east and west of anti-meridian, so you don't get edges that cross it. It also approximates geodesic edges with flat map edges. But it makes visualization system much easier.

lat lon coordinates (WGS84) conversion to local x, y plane

Currently I'm trying the following: I have points from google earth (WGS84) which I want to transform to a local x,y coordinate system: a tangential plane with y positive from south to north and x positive from west to east.
There is no need for the plane to be part of a global coordinate system more than the relation (x=0, y=0) = (lat,lon). The scale at which I'm working is in the order of say 100 kilometers (maximum of for example 200 km's). Very small errors (due to for example the curvature of the earth) are acceptable.
I have relatively little understanding of this topic as of yet. Can anybody help me out? Where would I need to look for example.
Thanks!
I haven't found the answer mathematically but have found that the package basemap (of the mpl_toolkit) should help with this respect (from wgs84 to a transverse mercator projection).

How to visualize 3d sun position (for solar power monitoring software)?

Im working on little hobby Raspberry Pi project. I'm measuring power and energy that comes from solar panel.
Im looking for better way of sun position visualisation.
My best idea so far that is easy to implement is something like this:
I found something really good:
(image source: link)
but I feel this is a bit too hard to implement.
Im looking for some kind of compromise between these two - easy to read for user and not so hard in implementation.
A bit lacking in requirements, but I like your first approach. I'm assuming the requirement includes a terminal-based interface, so I think you should use ASCII to render it. ;-)
*
\
\
50˚(\
---------+---------
E N W
Seriously, perhaps a graph with an X/Y axis showing the altitude and azimuth, combined with the first approach? Perhaps a graph similar to one of the ones on this page showing the progression of the sun today?
P.S. I'm marking this community wiki since I think this is, sadly, off-topic. =( You won't get MY close vote though!
Have you tried MatPlotLib in combination with PySolar?! With Pysolar you could easily get Azimuth and Zenith of Sun. With Matplotlib you can then draw an image to such.
This is how I would do it..
latitude, longitude = 53.280223, 12.236105
tilt_pv = 36.16 #tilt of PV panel.
azimuth_pv = 180. #North-South alignment of your PV panel. In this case 180° depicts that panel is facing South
baseDateTime = datetime(2015, 6, 9, 12, 0, 0) #Timestamp for 9 June 2015 12 UTC
zenith = Pysolar.GetAltitude(latitude, longitude, baseDateTime)
azimuth = Pysolar.GetAzimuth(latitude, longitude, baseDateTime)
That will give you the solar position.. That should go into your MatPlotLib configuration to plot this:
from mpl_toolkits.axes_grid.axislines import SubplotZero
import matplotlib.pyplot as plt
import numpy as np
if 1:
fig = plt.figure(1)
ax = SubplotZero(fig, 111)
fig.add_subplot(ax)
for direction in ["xzero", "yzero"]:
ax.axis[direction].set_axisline_style("-|>")
ax.axis[direction].set_visible(True)
for direction in ["left", "right", "bottom", "top"]:
ax.axis[direction].set_visible(False)
x = np.linspace(0., zenith, 1000) #straight line in 1000 steps
ax.plot(x, azimuth)
plt.show()
And while we are add it. I have written a Python program to forecast solar energy from GFS weather model (I use: Global Radiation, Wind Speed and Temperature) which is freely available. Would you be interested to run and test this?! I want to see if it is any good or where I need to rune the performance.

draw a population density map for the world in python matplotlib

I have a dictionary of countries (by their 2 letter country code), and the population density of each country.
I'd like to create a map with each country colored according to a color bar of density, with the country boundries drawn.
I've tried using the basemap package, like in this notebook
This piece of code works for the US countries. But I want to do it worldwide.