PostGIS - restrict result to the viewport of the map - php

I have imported few shp files into PostGIS/PostgreSQL DB. Now I want to display the same on the OSM map using OpenLayers. As there are millions of records for the selected county, I want to only fetch those geometries that are within the viewport of the map in the browser.
Also, how should I fetch this data into the map? Is KML a good option? I am using PHP at the backendn and would like to know if I can use it instead of installing a geo server.
I have tried to find an example on the net but couldn't find any. Please help.

Look at the BBOX Strategy for getting stuff within a bounding box. Then look at Vector layer for getting them on the map. I don't have an example right now, but if I find one I will complete my answer.
You can use PHP to process the answers if you want to keep it simple. Just use a PHP page as datasource for the layer, and with the BBOX strategy you will always get info on the bounding box and the PHP page can get the correct features.

Related

Table data extraction from image or scanned documents (Not pdf)

I want to extract the table data from images or scanned documents and map the header fields to their particular values mostly in an insurance document.I have tried by extracting them line by line and then mapping them using their position on the page. I gave the table boundary by defining a table start and end pivot, but it doesn't give me proper result, since headers have multiple lines sometimes (I had implemented this in php). I also want to know whether I can use machine learning to achieve the same.
For pdf documents I have used tabula-java which worked pretty well for me. Is there a similar type of implementation for images as well?
Insurance_Image
The documents would be of similar type as in the link above but of different service providers so a generic method of extracting such data would be very useful.
In the image above I want map values like Make = YAMAHA, MODEL= FZ-S, CC= 153 etc
Thanks.
I would definitively give a go to Tesseract, a very good OCR engine. I have been using it successfully in reading all sorts of documents embedded in emails (PDF, images) and a colleague of mine used it for something very similar to your use case - reading specific fields from invoices.
After you parse the document, simply use regex to pick the fields of interest.
I don't think machine learning would be particularly useful for you, unless you plan to build your own OCR engine. I'd start with existing libraries, they offer very good performance.
The easiest and most reliable way to do it without much knowledge in OCR would be this:
- Take an empty template for reference and mark the boxes coordinates that you need to extract the data from. Label them and save them for future use. This will be done only once for each template.
- Now when reading the same template, resize it to match the reference templates dimensions (If it's not already matching).
- You have already every box's coordinates and know what data it should contain (because you labeled them and saved them on the first step).
Which means that now you can just analyze the pixels contained in each box to know what is written there.
This means that given a list of labeled boxes (that you extracted in the first step), you should be able to get the data in each one of these boxes. If this data is typed and not hand written the extracted data would be easier to analyze or do whatever you want with it using simple OCR libraries.
Or if the data is always the same size and font like your example template above, then you could just build your own small database of letters of that font and size. or maybe full words? Depends on each box's possible answers.
Anyway this is not the best approach by far but it would definitely get the work done with minimal effort and knowledge in OCR.

jquery map dynamically create cells

I'm working on a basic map that is organized in the standard X/Y coordinate fashion. At each coordinate (x,y), there are contents that are stored in the database (the contents for each location is loaded via php/mysql database).
At my current setup, the map is just placed in the page as a static object, and I've used jquery/ajax to update the display and data for the coordinates.
I'm looking for a way to have a dynamically loaded click & Drag map, that will load cells as they are needed. (what ever comes into your view--in this case the window, should be loaded from the server--a div be created with the proper coordinates relative to its surrounding cells).
As of right now, my static map has this setup:
http://i.stack.imgur.com/LeVYO.gif (link because i cannot post pics)
The images are placed row by row (z index changing as each row goes down to give the appearance of overlap).
If that same pattern were to be duplicated, to dynamically create those cells, you can see that when a row sticks out on the right side, there is room on the left side, leaving easy overlapping.
I haven't been able to find any way to accomplish this:
1) When a cell (region with multiple coordinates inside) is visible, check to see if the surrounding 8 cells are already loaded.
2) If a surrounding 1 of 8 cells are not loaded, create the cell in the correct position, and assign the 'center coord' so that the generated data has a reference point.
3) Ensure that the cells are movable via click&drag, as well as the ability to click on the objects under (to interact).
4) Cells cannot be accessed by scrolling (scroll bar)
I have a feeling its fairly complicated, but I'm looking for the best way to do it, so ANY help or direction would be great!
With all my desire to help, I really cannot give the answer that will solve the problem, which I apologize for, but I think I can try to direct you to something.
It's the HTML 5's canvas and relative functions.
Look at that:
http://www.html5canvastutorials.com/advanced/html5-canvas-mouse-coordinates/
Maybe also you have to look at how a MAP is built with HTML 5
After tinkering, and lots of google searching, a quick search for "jquery infinite drag" returned this result:
http://ianli.com/infinitedrag/
I managed to use the function call provided when each cell is created, to gather map data from the database (ajax) and create the cells as needed.
http://mc1.empirebattles.com/map/jquery_map.html
I REALLY like the way its working so far, except that if i want to be able to click on anything under the map (for actions and such), the click event is called whenever i release the click for dragging the map around.... i've tried jquery's UI doubleclick, but it appears this plugin takes control as soon as double click is called
Anyways, incase anyone else needs something similar to this, this solution has helped me lots!

One huge XML file with long SQL statement or not?

I have a database with about 10 tables and they are all interconnected in some way(foreign keys, assosiative tables).
I want to use all that data to plot it on my instance of Google Map as markers with infoboxes and be able to filter the map.
Judging from the Google Maps Articles you have to use XML with the data from the database to plot the markers and everything else.
But what would be the right way to generate that XML file? Have a huge SQL statement to generate one XML file with entire data from all tables upon the load of the web-page and the map or is there a more correct approach for this?
You in no way have to use XML to place markers on an instance of Google Maps. You could, but you don't have to if it seems difficult. I work a lot with the Google Maps V3 API and I would recommend you export your data to JSON and embed it in your document using PHP or make it available for JavaScript to load using Ajax.
Creating interactive Markers from the data is REALLY easy. You just need to iterate over your data, create a Marker object for each point you want on the map, supply some HTML you want displayed in the info window and show that info window on the Marker's click event.
Instead of walking you through with teaspoon accuracy I'll refer you to the Google Maps API v3 beginner tutorial which among other things includes examples of how to create Markers and display them on the map.
Fun fact, you can control which icon is displayed for each marker (you can supply an URL to any image you want), as well as make them bounce. To summarize, you have way more control using JavaScript than if you went with XML.
Regarding performance, I would heed cillosis' advice and cache your MySQL data in which ever format you end up choosing. If you were to go with JSON you could cache the result of that as well. You can simply save the output in a file called something like "mysql-export-1335797013.json". That number is a Unix timestamp with which you can extrapolate when the data needs to be refreshed.
Use SQL the first time to generate the XML for a specific query, and then cache that XML output for later use. The very first time it may be slow, but after that it will already be generated and will be really fast.
If you want to use XML because PHP and AJAX make it relatively easy, then do. That's why the examples use it. But you are definitely not restricted to XML. JSON is commonly used because it's also easy with PHP, a smaller download than XML and delivered in a form which is directly usable by Javascript. Or you could use anything else which can be manipulated by your page's Javascript.
With regard to whether to use one humungous query and data download or not, you don't have to do that either. You could do: it might be slow — not only to do the query but also to transfer the data, where caching the query results won't help. Your users will have to wait for the data to arrive and then be manipulated by your Javascript to appear on the map.
You might consider doing a fairly simple query to get basic data to display so the users get something to see reasonably quickly, following that up with more queries, perhaps as data is required. There is no point in downloading loads of InfoWindow data if the user is not going to click and see it. In that instance, deliver marker data and basic InfoWindow data and only get detailed data if the user actually requests it (that is, use a two-stage InfoWindow).

Best way to take data and throw it in a graph?

I want to take data from a table that'll be almost exactly similiar to the one below, but have it in a line graph. The date values would be on the Y-axis, and it would plot the XP values on the X-axis. Since the numbers for each user vary, I'd need a way to make the distance between each point plotted "relative", I guess.
Example table http://img687.imageshack.us/img687/4175/tablel.png
Any suggestions?
If you want to generate a static image of the chart you could use Libchart or pChart for PHP.
It's better to get the data from the data source, not from the rendered table. Anyway it's two separate questions: how to take data from a table and how to draw a graph.
Drawing a chart in PHP is pretty easy. HTML/CSS can be used to draw a bar and PHP to calculate the bar length.
Or if you want to go the JavaScript route, maybe Raphaël.
I would go for HighCharts.
If you're using a linux/unix platform, you probably already have Gnuplot installed. Gnuplot can take a plot file and a datafile and generate an image, thus:
History.gnuplot, which needs to be generate first:
set title "Rawr_satch history"
set xlabel "Time"
set ylabel "Ranking"
set output "rawr_satch_graph.png"
set terminal png color
set xdata time
plot "rawr_satch_xphistory.dat" with linespoints
Which assumes a data file rawr_satch_xphistory.dat has already been generated, formatted thus:
2010-03-06 385581123
2010-03-05 384895430
2010-03-04 382983388
People have also written interfaces into Gnuplot for most languages, for example PHP-GNUPlot. Gnuplot scripts can be quite complex, and you could plot multiple variables, etc.
I use and fully recommend the Google Chart Tools API. If you're looking for a simple line-graph, they provide a very simple API that you can call that requires no installation or configuration. Some of the documentation is confusing, but I've been able to figure it out with a little bit of patience.
All that is required is an IMG tag and point the src to the Google URL with the right parameters.
They also have a more interactive Javascript library if you want to provide more functionality later on.
I've used the following from PHP: Google Charts, Open Flash Chart, YUI charts (Flash), and AMCharts (also Flash).
They're all pretty easy to use and all have their pros and cons. If you want to show more than a single graph on one page, don't use Flash-based charts. It turns out browsers have trouble displaying a dozen Flash charts all at once. AMCharts is probably the most feature rich, but the options available depend on which of their chart packages you select, and configuration is pretty complex.

Google Maps Overlays

I'm trying to find something, preferably F/OSS, that can generate a Google Maps overlay from KML and/or KMZ data.
We've got an event site we're working on that needed to accommodate ~16,000 place markers last year and will likely have at least that many again this year. Last year, the company that had done the site just fed the KML data directly to the gMaps API and let it place all of the markers client side. Obviously, that became a performance nightmare and tended to make older browsers "freeze" (or at least appear frozen for several minutes at a time).
Ideally this server side script would take the KML, the map's lat/lon center, and the map zoom level and appropriately merge all of the visible place markers into a single GIF or PNG overlay.
Any guidance or recommendations on this would be greatly appreciated.
UPDATE 10/8/2008 - Most of the information I've come across here and other places would seem to indicate that lessening the number of points on the map is the way to go (i.e. using one marker to represent several when viewing from a higher altitude/zoom level). While that's probably a good approach in some cases, it won't work here. We're looking for the visual impact of a US map with many thousand markers on it. One option I've explored is a service called PushPin, which when fed (presumably) KML will create, server side, an overlay that has all of the visible points (based on center lat/lon and zoom level) rendered onto a single image, so instead of performing several thousand DOM manipulations client side, we merge all of those markers into a single image server side and do a single DOM manipulation on the client end. The PushPin service is really slick and would definitely work if not for the associated costs. We're really looking for something F/OSS that we could run server side to generate that overlay ourselves.
You may want to look into something like Geoserver or Mapserver. They are Google map clones, and a lot more.
You could generate an overlay that you like, and Geoserver(I think mapserver does as well) can give you KML, PDF, png, and other output to mix your maps, or you could generate the whole map by yourself, but that takes time.
Not sure why you want to go to a GIF/PNG overlay, you can do this directly in KML. I'm assuming that most of your performance problem was being caused by points outside the user's current view, i.e. the user is looking at New York but you have points in Los Angeles that are wasting memory because they aren't visible. If you really have 16,000 points that are all visible at once for a typical then yes you'll need to pursue a different strategy.
If the above applies, the procedure would be as follows:
Determine the center & extent of the map
Given that you should be able to calculate the lat/long of the upper left and lower right corners of the map.
Iterate through your database of points and check each location against the two corners. Longitude needs to be greater (signed!) than the upper left longitude and less than the lower right longitude. Latitude needs to be less than the upper left latitude (signed!) and greater than the lower right latitude. Just simple comparisons, no fancy calculations required here.
Output the matching points to a temporary KML for the user.
You can feed KML directly into Google Maps and let it map it, or you can use the Javascript maps API to load the points via KML.
It might not solve your exact problem here, but for related issues you might also look into the Google Static Maps API. This allows you to create a static image file with placemarkers on it that will load very quickly, but won't have the interactivity of a regular Google map. Because of the way the API is designed, however, it can't handle anywhere near 16,000 points either so you'd still have to filter down to the view.
I don't know how fare you are with your project but maybe you can take a look at GeoDjango? This modified Django release includes all kinds of tools to store locations; convert coordinates and display maps, the easy way. Offcourse you need some Python experience and a server to run it on, but once you've got the hang of Django it works fast and good.
If you just want a solution for your problem try grouping your results at lower zoom levels, a good example of this implementation can be found here.
This is a tough one. You can use custom tilesets with Google Maps, but you still need some way to generate the tiles (other than manually).
I'm afraid that's all I've got =/
OpenLayers is a great javascript frontend to multiple mapping services or your own map servers. Version 2.7 was just released, which adds some pretty amazing features and controls.

Categories