Showing posts with label CityData. Show all posts
Showing posts with label CityData. Show all posts

Aug 20, 2010

Visualize weather pattern with random walk

When talking about the “good” weather, the daily temperature change is important, slow steady change from day to day is much better than sudden temperature increase or drop.

Here is the daily mean temperature for Bloomington, Indiana.

bloomington

We define a piecewise function to classify the day to day temperature change. x is (T – Tprevious day), y is the threshold to define the weather change: 1 ~ no change, 2 ~ cold move, 3 ~ mild cold, 4 ~ mild hot, 5 ~ hot move.

piecewise

Then we can apply the same random walk trick used in the post Visualize irrational number as random walk. The classification results are mapped into the moves: 1 ~ no move {0, 0}, 2 ~ move south {0, –1}, 2 ~ move west {-1, 0}, 3 ~ move east {1, 0}, 4 ~ move north {0, 1}. Then we can get a random walk image (the green dot is starting point {0, 0}, the red one is the end).

bloomingtonwalk

This is probably nothing interesting. However, we can use it to compare the patterns among different cities. Here is the results from Washington, Columbus, Indianapolis, Kansas City, Denver. The threshold is 3.5 degree. This shows the certain pattern among cities from ocean to inland.

cities

When we compare the cities across the ocean, a different pattern is shown.

cities2

Down the weatherwalk.nb for the detail.

Jun 17, 2009

Mathematica 7: Export ESRI shapefile

One way to achieve this goal is by using the spatial database. Oracle and PostgreSQL have excellent supports of spatial data types. All you need to do is to connect the database in Mathematica and insert the data into the spatial database. There are tools come with databases allow you to dump the data into a shapefile. If you need a light-weighted spatial database, you can try SpatiaLite, it is based on popular SQLite.

For Windows platform, download the followings first:

spatialite-tools and init_spatialite-2.3.sql

upzip them and copy spatialite.exe and init_spatialite-2.3.sql into the same folder.

In this example, we like to export the following information to a shape file:

data={First[#], CityData[#,"Population"], CityData[#,"Longitude"], CityData[#,"Latitude"]} &/@ CityData [{All, "Indiana", "UnitedStates"}];

We need write some SQLs:

CREATE TABLE Towns (Name TEXT, Population INTEGER);
SELECT AddGeometryColumn('Towns','LonLat', 4326,'Point',2);

4326 means  EPSG 4326, the coordinate reference system of WGS84(longitude, latitude) pair coordinates in degrees.

To Insert the data:

INSERT INTO Towns (Name, Population, LonLat) Values ('Indianapolis', 784118, GeomFromText ('Point(-86.1477 39.7909)', 4326));

In Mathematica, this will create all the “INSERT” statements

str="INSERT INTO Towns (Name, Population, LonLat) Values ('v1', v2, GeomFromText('Point(v3 v4)',4326));"

strs=StringReplace[str, {"v1"->#[[1]], "v2"->ToString[#[[2]]],"v3"->ToString[#[[3]]], "v4"->ToString[#[[4]]]}] &/@ data;

Export["insert.txt", strs]

Then we can put all the SQL statements together into one file (test.sql):

BEGIN;
CREATE TABLE Towns (Name TEXT, Population INTEGER);
SELECT AddGeometryColumn('Towns','LonLat', 4326,'Point',2);
… INSERT INTO commands is here …
COMMIT;

In window command line mode:

spatialite.exe -init init_spatialite-2.3.sql test.sqlite < test.sql

So far, we have got all the data from Mathematica into the table Towns in test.sqlite.

run: spatialite.exe test.sqlite, and check if the records inside are right, then dump the shapefile:

.dump Towns LonLat towns_shp ASCII POINT;

Import the town_shp.shp back into Mathametica:

Show[Import["towns_shp.shp"], Frame -> True, FrameTicks -> Automatic]

towns

More information: SpatiaLite Tutorial

If you are not comfortable with command-line tools, there is a GUI tool for SpatiaLite.

Mar 24, 2009

Testing the new map with Mathematica

Generally speaking, when dealing with GIS data, you probably shouldn’t put Mathematica in top of your list.  However, when you want to develop a new algorithm or visualization, Mathamtica is a good choice to test the prototype.

Here is an example:

newmap01  

The same map in the “forest” looking.

newmap2

Feb 25, 2008

Google Static Maps API

Google finally release Google Static Maps API, which allows you to generate the maps using a regular URL (ala REST) along with parameters specifying location, size, etc and it returns a unique GIF image with that map.
However, there are several drawbacks, for example, 512x512 is the largest image size allowed and geocoding is not integrated. Even though, it is better than nothing.

The following shows the API example:
Top 10 "Mathematica" cites from Google Trends.


For fun, top 10 Mathematica cities vs top 10 Matlab cities
Red "M"--> Mathematica City, Blue "T" --> Matlab City, Green "B" --> City with both Mathematica and Matlab.


Update: static maps seems use the old data
Noticed the problem with "Trade Center Dr",
Check this place on Google Map.

Feb 7, 2008

Select Point Data inside Geographical Boundary

Here is an example on how to select point data inside the geographical boundary by using point inside polygon function.

(* all the cities {name, coordinates} in Indiana *)
allcities = {First[#], CityData[#, "Coordinates"]} & /@ CityData[{All, "Indiana", "UnitedStates"}];


We like to select the cities inside St. Joseph county.
Boundary of St. Joseph county is imported as Polygon. See details on importing GIS data.

In Mahtematica-users Wiki, there is an discussion on Point Inside Polygon. We choose the first solution, since it works on non-convex polygon.

(* select cities by Point inside Polygon function *)
Select[allcities, PLSPolygon[countyboundary, #[[2]]] &]


Dec 20, 2007

Common city names shared between USA and Mexico

m = Take[First /@ CityData[{All, #}]] & /@ {"UnitedStates", "Mexico"};
cityname = Intersection[m[[1]], m[[2]]]



{"AguaDulce", "Alamo", "Algodones", "Alvarado", "Anahuac", "Belen", "Camargo", "Carbon", "Carmen", "Cerritos", "CerroGordo", "Chalco", "China", "Colon", "Concordia", "Delta", "Duarte", "Durango", "Eldorado", "ElRefugio", "Gonzalez", "Guadalupe", "Hercules", "Hidalgo", "LaJoya", "LaPalma", "LaPaz", "LaPresa", "Leon", "LosAltos", "Madera", "Madrid", "Magdalena", "Martinez", "Medina", "Mina", "Miramar", "Naco", "Nogales", "Oriental", "PaloAlto", "Progreso", "RioBravo", "RioGrande", "Rodeo", "Salamanca", "Salinas", "Saltillo", "SanBernardino", "SanBuenaventura", "SanCarlos", "SanFelipe", "SanFernando", "SanGabriel", "SanIgnacio", "SanLucas", "SanLuis", "SanMarcos", "SanPatricio", "SanPedro", "SanRafael", "SantaAna", "SantaBarbara", "SantaClara", "SantaTeresa", "Soledad", "Sonoita", "Tampico", "Toluca", "Torreon", "Valparaiso", "Victoria"}

Dec 19, 2007

Top 10 most common city names in USA

(*Total 28184 USA city names *)
Take[SortBy[Tally[First /@ CityData[{All, "UnitedStates"}]], Last], -10]

{{"Fairview", 21}, {"Georgetown", 21}, {"Marion", 21}, {"Greenville", 23}, {"Madison", 23}, {"Salem", 24}, {"Springfield", 24}, {"Washington", 25}, {"Clinton", 26}, {"Franklin", 28}}

Where are the "Clinton"?

Graphics[{LightBlue, CountryData["UnitedStates", "Polygon"],
PointSize[Medium], Red,
Point[Reverse[CityData[#, "Coordinates"]]] & /@
CityData[{"Clinton", "UnitedStates"}]}]