#!/bin/sh
#############################################################################
#
# e002shp.sh
#
# SCRIPT:   	e00 to shape conversion
# AUTHOR(S):	Paolo Zatelli - 2006/03/02
# PURPOSE:  	Automatic e00 to shape conversion in GRASS
#   	    	
# COPYRIGHT:	2006 Paolo Zatelli
# LICENSE:	GPL 2.0 or (at your option) any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#############################################################################
# e002shp.sh automatically converts all the *.e00 ESRI vector files in a directory
# to ESRI Shapefile vector file in GRASS.
# It uses GRASS v.in.e00 e v.out.ogr modules, so avcimport and e00conv (for 
# compressed E00) must be installed and available to GRASS as well as the OGR library.
# IMPORTANT: the script must be run from the GRASS shell in the directory 
# containing the E00 file.

# The primitive variable must be set to the geometric primitive type to be written
# in the output shapefile, possible values are: point,line,boundary,centroid,area.
# Multiple choices are not allowed, only line and boundary or point and centroid 
# can be selected together.
# Uncomment below the feature type(s) to be written into the shapefile.

#primitive=point,centroid
primitive=line,boundary
#primitive=area

# The prefix is used for the shapefile names; each shapefile name is formed from
# the prefix and the name of the E00 input file.
# Beware of the DBF file name limitations.
prefix=test_

#############################################################################
# nothing to modify under this line

START_DATE=`date`
num=0

for namein in *e00
	do

let "num=$num+1"

col1=`expr index "$namein" /`
col2=`expr length "$namein"`

let "col2=$col2-4"
let "lungh=$col2-$col1"

#echo $col1:$col2:$lungh

# strips the file name from the complete path+filename string
nameout=${namein:col1:lungh}

#echo $namein, $nameout

# remove the map in GRASS if exists, v.in.e00 does not overwrite maps
g.remove vect=$prefix$nameout
# E00 import
v.in.e00 file=$namein type=$primitive vect=$prefix$nameout
# Shape output
v.out.ogr input=$prefix$nameout type=$primitive dsn=./ olayer=$prefix$nameout layer=1 format=ESRI_Shapefile
# remove the GRASS map
g.remove vect=$prefix$nameout

done 

STOP_DATE=`date`
echo $num " files processed"
echo "Start: "$START_DATE
echo "Stop: "$STOP_DATE

