Extract addresses zip street city form openstreetmap pbf files - export

to build up a database of German cities where ic can query cities around a cup with a defined distance, i want wo export from pbf files the address data into a mysql database. I already found a good source where i can get the pbf files, but could not find a tool to export the data i want/need. perhaps somebody has had already the same problem and can help me out wit a link here.
i only need the zip, city, street and lat/lon, no map data or vector data
Georg

You can use imposm to import data to a PostGIS database. Maybe it is an option to use PostgreSQL instead of MySQL, because PostgreSQL is more common in the OpenStreetMap world and you might find more tools for this.
You can customize the imposm schema to leave our the geo data you do not need.
Another way would be to use osmosis:
https://gis.stackexchange.com/questions/185793/is-there-a-way-to-retrieve-all-cities-in-the-world-from-openstreetmap
There is a --write-mysql switch:
https://github.com/openstreetmap/osmosis/blob/master/doc/detailed-usage.adoc#write-mysql-wm
So something like this should work:
./osmosis --read-pbf germany-latest.osm.pbf \
--tf accept-nodes place=city,town,village \
--tf reject-relations --tf reject-ways --lp \
--write-mysql database=test user=*** password=***

Related

How to construct my own Datasets for TransE Algorithm in a specific knowledge graph

now I am building a knowledge graph of the Chinese stock and want to build a news recommendation system. And I want to use TransE algorithm for the entity embedding and relationship embedding. ​But I do not have the dataset and don't know clearly how to build a dataset using my own knowledge graph?
One start would be to use data from Wikidata. It has some information on Chinese companies (I suppose you are referring to companies listed on Chinese stock exchanges). For instance, https://www.wikidata.org/wiki/Q831445 displays information about Sinopec.
The data from Wikidata can be downloaded from the API, the large dumps files at https://dumps.wikimedia.org/wikidatawiki/ or the SPARQL endpoint at https://query.wikidata.org/.
You can get a list of companies listed on the Shenzhen Stock Exchange with the SPARQL query:
SELECT
?company ?companyLabel
?industry ?industryLabel
{
?company wdt:P414 wd:Q517750 .
OPTIONAL { ?company wdt:P452 ?industry }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,zh". }
}
The result is (also) available at https://w.wiki/9DM . This result can be extended by modifying the query and it can be downloaded in various formats. With the DESCRIBE SPARQL keyword you can get the triple format that may be useful for the TransE algorithm, e.g., DESCRIBE wd:Q831445 with the result at https://w.wiki/9DN .
It is possible to process the large dump files and make a knowledge graph embedding with Gensim's Word2Vec, see "Wembedder: Wikidata entity embedding web service" at https://arxiv.org/abs/1710.04099 . You can explore one result of this approach with the Wembedder webapp, e.g., https://tools.wmflabs.org/wembedder/most-similar/Q51747 displays the result of a "most similar" query in the knowledge graph embedding with Air China

Where are the links to each media file in a whatsapp database

I want to organize an album with all the photos from a Whatsapp group.
With a rooted phone I have downloaded the unencrypted whatsapp databases to my computer.
With Sqliteman on Ubuntu I've looked around, specially in the msgstore.db and can't seem to find all of the links to the images of a particular group.
With the table "chat_list" I saw that a specific "key_remote_jid" value corresponded to the group I wanted and in the "messages" database I could use that "key_remote_jid" to get all the messages related to that group with something like:
select * from messages where key_remote_jid="53151151515-1515131313535#g.us";
Furthermore I can discard text messages in order to focus on the media I want (Photos and Videos mainly) by
select * from messages where key_remote_jid="53151151515-1515131313535#g.us" and not media_wa_type = 0;
And some of the images there have a filename under the column "media_name" but they are only the ones I have sent.
There is another table called "media_refs" which contains many paths of images and video but seems to only include files which have been forwarded to more than one chat and has a key which I haven't found how to relate to the "messages" table.
The group is rather old (dating back to 2013) and it seems likely the way whatsapp handles its database has changed through the years as there are some columns which are consistently {null} in the past and not in the most recent messages (like participant_hash) and the value key_id seems to change the format at some point.
However the Whatsapp app has no problem in showing me the a file from any moment in the past and giving me the option to open the actual file with a "Gallery"-type software, so the information is in my phone somewhere.
The point of all this is to basically have a backup I can work with and be able to delete the images from my phone and still have them organized in the same way they were shared in that group.
I found the link of Whatsapp Images in column thumb_image of table messages as a blob. You can view the blob in hex and look for the the strings which may looks like this Whatsapp Images\IMG-20130101-WA0001.jpg. I don't know exactly where Whatsapp store the links properly, but this works for me.
Here is how I managed to extract the links:
select
_id,
media_mime_type,
substr(
substr(thumb_image,instr(thumb_image,'IMG'),60)
,instr(substr(thumb_image,instr(thumb_image,'IMG'),23),'IMG')
,23)
from `messages`
where
`key_remote_jid` like '%<your desired contact>%'
and `media_mime_type` like '%image%';
I use DB Browser for SQLite to do this in Windows.
(Android) - what one is looking for is file_path within the table message_media. It points to files relative to /sdcard/WhatsApp/ and its message_row_id references the messages table pk _id. A where condition on the key_remote_jid of the group (<..>#g.us) should output all group image filepaths.
select mm.file_path
from messages m
join message_media mm on m._id = mm.message_row_id
where m.key_remote_jid = 'xyz#g.us'
and m.media_wa_type = 1; -- 1 = images
(thumbnail previews are stored as blob bytes in raw_data in the messages table itself)

How to get the bounding coordinates for a US postal(zip) code?

Is there a service/API that will take a postal/zip code and return the bounding(perimeter) coordinates so I can build a Geometry object in a MS SQL database?
By bounding coordinates, I mean I would like to retrieve a list of GPS coordinates that construct a polygon that defines the US zip code.
An elaboration of my comment, that ZIP codes are not polygons....
We often think of ZIP codes as areas (polygons) because we say, "Oh, I live in this ZIP code..." which gives the impression of a containing region, and maybe the fact that ZIP stands for "Zone Improvement Plan" helps the false association with polygons.
In actuality, ZIP codes are lines which represent, in a sense, mail carrier routes. Geometrically, lines do not have area. Just as lines are strings of points along a coordinate plane, ZIP code lines are strings of delivery points in the abstract space of USPS-designated addresses.
They are not correlated to geographical coordinates. What you will find, though, is that they appear to be geographically oriented because it would be inefficient for carriers to have a route completely irrelevant of distance and location.
What is this "abstract space of USPS-designated addresses"? That's how I am describing the large and mysterious database of deliverable locations maintained by the US Postal Service. Addresses are not allotted based on geography, but on the routes that carriers travel which usually relates to streets and travelability.
Some 5-digit ZIP codes are only a single building, or a complex of buildings, or even a single floor of a building (yes, multiple zip codes can be at a single coordinate because their delivery points are layered vertically). Some of these -- among others -- are "unique" ZIPs. Companies and universities frequently get their own ZIP codes for marketing or organizational purposes. For instance, the ZIP code "12345" belongs to General Electric up in Schenectady, NY. (Edit: In a previous version of Google Maps, when you follow that link, you'd notice that the placement marker was hovering, because it points to a ZIP code, which is not a coordinate. While most US ZIP codes used to show a region on Google Maps, these types cannot because the USPS does not "own" them, so to speak, and they have no area.)
Just for fun, let's try verifying an address in a unique ZIP code. Head over to SmartyStreets and punch in a bogus address in 12345, like:
Street: 999 Sdf sdf
ZIP Code: 12345
When you try to verify that, notice that... it's VALID! Why? The USPS will deliver a piece to the receptacle for that unique ZIP code, but at that point, it's up to GE to distribute it. Pretty much anything internal to the ZIP code is irrelevant to the USPS, including the street address (technically "delivery line 1"). Many universities function in a similar manner. Here's more information regarding that.
Now, try the same bogus address, but without a ZIP code, and instead do the city/state:
Street: 999 Sdf sdf
City: Schenectady
State: NY
It doesn't validate. This is because even though Schenectady contains 12345, where the address is "valid," it geometrically intersects with the "real" ZIP codes for Schenectady.
Take another instance: military. Certain naval ships have their own ZIP codes. Military addresses are an entirely different class of addresses using the same namespace. Ships move. Geographical coordinates don't.
ZIP precision is another fun one. 5-digit ZIP codes are the least "precise" (though the term "specific" might be more meaningful here, since ZIP codes don't pinpoint anything). 7- and 9-digit ZIP codes are the most specific, often down to block or neighborhood-level in urban areas. But since each ZIP code is a different size, it's really hard to tell what actual distances you're talking.
A 9-digit ZIP code might be portioned to a floor of a building, so there you have overlapping ZIP codes for potentially hundreds of addresses.
Bottom line: ZIP codes don't, contrary to popular belief, provide geographical or boundary data. They vary widely and are actually quite un-helpful unless you're delivering mail or packages... but the USPS' job was to design efficient carrier routes, not partition the population into coordinate regions so much.
That's more the job of the census bureau. They've compiled a list of cartographic boundaries since ZIP codes are "convenient" to work with. To do this, they sectioned bunches of addresses into census blocks. Then, they aggregated USPS ZIP code data to find the relation between their census blocks (which has some rough coordinate data) and the ZIP codes. Thus, we have approximations of what it would look like to plot a line as a polygon. (Apparently, they converted a 1D line into a 2D polygon by transforming a 2D polygon based on its contents to fit linear data -- for each non-unique, regular ZIP code.)
From their website (link above):
A ZIP Code tabulation area (ZCTA) is a statistical geographic entity
that approximates the delivery area for a U.S. Postal Service
five-digit or three-digit ZIP Code. ZCTAs are aggregations of census
blocks that have the same predominant ZIP Code associated with the
addresses in the U.S. Census Bureau's Master Address File (MAF).
Three-digit ZCTA codes are applied to large contiguous areas for which
the U.S. Census Bureau does not have five-digit ZIP Code information
in its MAF. ZCTAs do not precisely depict ZIP Code delivery areas, and
do not include all ZIP Codes used for mail delivery. The U.S. Census
Bureau has established ZCTAs as a new geographic entity similar to,
but replacing, data tabulations for ZIP Codes undertaken in
conjunction with the 1990 and earlier censuses.
The USCB's dataset is incomplete, and at times inaccurate. Google still has holes in their data, too (the 12345 is a somewhat good example) -- but Google will patch it eventually by going over each address and ZIP code by hand. They do this already, but haven't made all their map data perfect quite yet. Naturally, access to this data is limited to API terms, and it's very expensive to raise these.
Phew. I'm beat. I hope that helps clarify things. Disclaimer: I used to be a developer at SmartyStreets. More information on geocoding with address data.
Even more information about ZIP codes.
What you are asking for is a service to provide "Free Zip code Geocoding". There are a few out there with varying quality. You're going to have a bad time coding something like this yourself because of a few reasons:
Zip codes can be assigned to a single building or to a post office.
Zip codes are NOT considered a polygonal area. Projecting Zip codes to a polygonal area will require you to make an educated guess where the boundary is between one zipcode and the next.
ZIP code address data specifies only a center location for the ZIP code. Zip code data provides the general vicinity of an address. Mailing addresses that exist between one zipcode and another can be in dispute on which zipcode it actually is in.
A mailing address may be physically closer to zipcode 11111, yet its official zip code is a more distant zip code point 11112.
Google Maps has a geocoding API:
The google maps API is client-side javascript. You can directly query the geocoding system from php using an http request. However, google maps only gives you what the United States Postal Service gives them. A point representing the center of the zipcode.
https://developers.google.com/maps/#Geocoding_Examples
map city/zipcode polygons using google maps
Thoughts on projecting a zipcode to its lat/long bounding box
There are approximately 43,000 ZIP Codes in the United States. This number fluctuates from month to month, depending on the number of changes made. The zipcodes used by the USPS are not represented as polygons and do not have hard and fast boundaries.
The USPS (United States Postal Service) is the authority that defines each zipcode lat/long. Any software which resolves a zipcode to a geographical location would be in need of weekly updates. One company called alignstar provides demographics and GIS data of zipcodes ( http://www.alignstar.com/data.html ).
Given a physical (mailing) address, find the geographical coordinates in order to display that location on a map.
If you want to reliably project what shape the zipcode is in, you are going to need to brute force it and ask: "give me every street address by zipcode", then paint boxes around those mis-shapen blobs. Then you can get a general feel for what geographical areas the zipcodes cover.
http://vterrain.org/Culture/geocoding.html
If you were to throw millions of mailing address points into an algorithm resolving every one to a lat/long, you might be able to build a rudimentary blob bounding box of that zipcode. You would have to re-run this algorithm and it would theoretically heal itself whenever the zipcode numbers move.
Other ideas
http://shop.delorme.com/OA_HTML/DELibeCCtpSctDspRte.jsp?section=10075
http://www.zip-codes.com/zip-code-map-boundary-data.asp
step 1:download cb_2018_us_zcta510_500k.zip
https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html
if you want to keep them in mysql
step 2: in your mysql create a db of named :spatialdata
run this command
ogr2ogr -f "MySQL" MYSQL:"spatialdata,host=localhost,user=root" -nln "map" -a_srs "EPSG:4683" cb_2018_us_zcta510_500k.shp -overwrite -addfields -fieldTypeToString All -lco ENGINE=MyISAM
i uploaded the file on github(https://github.com/sahilkashyap64/USA-zipcode-boundary/blob/master/USAspatialdata.zip)
In the your "spatialdata db" there will be 2 table named map & geometry_columns .
In 'map' there will be a column named "shape".
shape column is of type "geometry" and it contains polygon/multipolygon files
In 'geometry_columns' there will will be srid defined
how to check if point falls in the polygon
SELECT * FROM map WHERE ST_Contains( map.SHAPE, ST_GeomFromText( 'POINT(63.39550 -148.89730 )', 4683 ) )
and want to show boundary on a map
select zcta5ce10 as zipcode, ST_AsGeoJSON(SHAPE) sh from map where ST_Contains( map.SHAPE, ST_GeomFromText( 'POINT(34.1116 -85.6092 )', 4683 ) )
"ST_AsGeoJSON" this returns spatial data as geojson.
Use http://geojson.tools/
"HERE maps" to check the shape of geojson
if you want to generate topojson
mapshaper converts shapefile to topojson (no need to convert it to kml file)
npx -p mapshaper mapshaper-xl cb_2018_us_zcta510_500k.shp snap -simplify 0.1% -filter-fields ZCTA5CE10 -rename-fields zip=ZCTA5CE10 -o format=topojson cb_2018_us_zcta510_500k.json
If you want to convert shapefile to kml
`ogr2ogr -f KML tl_2019_us_zcta510.kml -mapFieldType Integer64=Real tl_2019_us_zcta510.shp
I have used mapbox gl to display 2 zipcodes
example: https://sahilkashyap64.github.io/USA-zipcode-boundary/
code :https://github.com/sahilkashyap64/USA-zipcode-boundary
SQL Server Solution
Download the Shape files from the US Census:
https://catalog.data.gov/dataset/2019-cartographic-boundary-shapefile-2010-zip-code-tabulation-areas-for-united-states-1-500000
I then found this repository to import the shape file to SQL Server, it was very fast and required no additional coding: https://github.com/xfischer/Shape2SqlServer
Then I could write my own script to find out which zip codes are in a polygon I created:
DECLARE #polygon GEOMETRY;
DECLARE #isValid bit = 0;
DECLARE #p nvarchar(2048) = 'POLYGON((-120.1547 39.2472,-120.3758 39.1950,-120.2124 38.7734,-119.6590 38.8162,-119.6342 39.3672,-120.1836 39.2525,-120.1547 39.2472))'
SET #polygon = GEOMETRY::STPolyFromText(#p,4326)
SET #isValid = #polygon.STIsValid()
IF (#isValid = 1)
SET #polygon = #polygon.MakeValid();
SET #isValid = #polygon.STIsValid()
IF (#isValid = 1)
BEGIN
SELECT * FROM cb_2019_us_zcta510_500k
WHERE geom.STIntersects(#polygon) = 1
END
ELSE
SELECT 'Polygon not valid'
I think this is what you need it uses US Census as repository: US Zipcode
Boundaries API: https://www.boundaries-io.com
Above API shows US Boundaries(GeoJson) by zipcode,city, and state. you should use the API programatically to handle large results.
Disclaimer,I work here
I think the world geoJson link and the google map geocode api can help you.
example: you can use the geocode api to code the zip,you will get the city,state,country,then,you search from the world and us geoJson get the boundry,I have an example of US State boundry,like dsdlink

How to convert other source maps to Obf ( Eg Navteq,garmin,tom Tom)

I am looking for a solution to create a better OSMAND map for a region , as existing OSM maps are not complete.
If I get a map in Navteq/Garmin/Tom Tom format, will I be able to convert it to Osmand OBF format and replace in Osmand ?
There seems to be less references to this topic.
I would expect Navteq/Garmin/Tom Tom would give maps in shapefile format, and OSM can take data from shapefiles per http://wiki.openstreetmap.org/wiki/Shapefiles#Obtaining_OSM_data_from_shapefiles
HOWEVER... Navteq, Garmin, and Tom Tom are commercial organizations which value their data; I would be very surprised if their license would allow you to give it to OSM for free use. Furthermore, I expect that OSM would not allow you to give it to them. So I would be very surprised if this approach worked.

Free GeoData (Capitals, POIs, ...) database

Does anybody know a free geodata database that can be also be used for commercial purposes (something that is licenced under Creative Commons license)?
I'd need GeoData like capitals, poi's, .. all around the world. The data should at least include Name, Country, Coordinates (lat / lon).
thx :)
Try GeoNames. It doesn't have POI but otherwise it is very complete.

Resources