-2

have this csv file and I wish to import it in my MySQL database to draw all polygons I want simply by typing a postcode. In this file I have

  • the postcode
  • a list of coordinates (my idea is to use all coordinates to draw an accurate polygon
  • a field called 'area' (I have no idea how to use this information)

My first problem is how to parse this file to save data in my DB to have 3 columns

|postcode|latitude|longitude

Can you help with this issue please? Or suggest me a better way if you know.

Thanks.

Edit: I am trying to use this php code editing each time the string

$fname = "uk.csv";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace('"<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>', '', $content);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);

but this file is homogeneous because not all rows start with the same string

<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>

but also with only

<Polygon><outerBoundaryIs><LinearRing><coordinates>
Tab
  • 309
  • 2
  • 7
  • 16

1 Answers1

1

This isn't only a string, it's valid XML.

Use DOM-methods to parse the String.

simple example:

  //create DOMDocument
$doc=new DOMDocument();
  //load the source from string
$doc->loadXML('<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>0.167014,51.79976,0.0 0.167634,51.79976,0.0 0.177091,51.796733,0.0 0.181105,51.793738,0.0</coordinates></LinearRing></outerBoundaryIs></Polygon></MultiGeometry>');
  //fetch the coordinates-nodes
$coords=$doc->getElementsByTagName('coordinates');
  //get the content of the first <coordinates/>
echo $coords->item(0)->nodeValue;
  //returns:
  //0.167014,51.79976,0.0 0.167634,51.79976,0.0 0.177091,51.796733,0.0 0.181105,51.793738,0.0
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • thank you for your answer @Dr.Molle but the problem is not all rows start with 'MultiGeometry' tag: some rows start with 'Poligon' instead of 'MultiGeometry – Tab Nov 03 '13 at 09:44
  • It doesn't matter how it starts , the example accesses the ``-nodes directly – Dr.Molle Nov 03 '13 at 09:48
  • I'm not being able to correctly parse the file. I have this file called 'uk.csv'. I tried to put it in loadXML() but it dosn't works. My intention is to parse the file to have, such as in my initial post, a table of this type |postcode|latitude|longitude foreach postcode, obviously, I can have more couple of lat/lng – Tab Nov 03 '13 at 10:09
  • The file contains CSV, use `fgetcsv` to extract the columns out of the lines. And of course you have multiple LatLng's, Polygons are defined by multiple points. How do you expect to be able to draw a polygon by a single point? – Dr.Molle Nov 03 '13 at 10:27
  • I know to draw a polygon many point are required: this is my reason for that I wish to extract all coordinates (lat/long) foreach postcode and save all in DB in way to have, forearch postcode, many coordinates to draw a polygon foreach postcode. I try to use fgetcsv – Tab Nov 03 '13 at 10:33
  • I don't understand why you don't consider using the FusionTable directly, like [this](http://geocodezip.com/v3_FusionTables_UKpostcode_map.html) (which uses GVis) or a FusionTablesLayer directly. – geocodezip Nov 04 '13 at 02:31